I'm coding a text adventure in python.
In general: I want people to be able to make mistakes for putting in the wrong answers. If they not write a valid answer like "1" or "2" they should get send back with a loop until they put in a viable answer without breaking the whole code and to start all over again.
As mentioned in the title: I have a problem with consecutive if-query-loops (with elif else etc.) I do found a nice while-function for just one query. (here on stack overflow. THX for that so far) But trying to implement it for consecutive if-querys leading to the next one is not working. The first if-query works fine, but after going to the second one, when writing gibberish for example, instead of looping back to the second question I get send to the first(!) one again...
I tried to put "else: -continue..." to every(!) if-query, even with multiple "while True:" after each query.. but that resulting in infinty loops...
I'm a beginner with python, but heard that maybe switch / case -functions might be working?
Still, is there maybe a more efficient way?
Thanks a lot for your time and effort! :-)
while True:
question1 = input("question1")
if question1 =="1":
print("Wrong")
break
elif question1 == "2":
print("Right, go on!!")
question2 = input("question2")
if question2 == "1":
print("Wrong")
break
elif question2 == "2":
print("Right, go on!!")
question3 = input("question3")
if question3 == "1":
print("wrong")
break
elif question3 == "2":
print("Right, go on!!")
quit()
else:
print("Use a valid answer!")
continue
Here's an idea of how I'd structure your program and what things you should look at and learn to properly implement what you want to do.
I'd recommend you use a state machine and putting all your logic into a dictionary (which later you could read from a json file, so you can create different adventures without having to modify the code at all)
The logic of your adventure is:
Then you can mount each question with a nested dictionary structure, like this:
You can add more options to your dictionary, maybe you want the user to allow more answer for each question, and you can then put to which state each answer goes (maybe you don't want sequential questions, but more a graph with complex paths)
Now your main code will have a variable state that you can initialize to 1
You can then access the information of this state by doing
For example the question of the current state is
And the answers dictionary are in
So if the user inputs an answer
answer, you can get the appropriate response doingThis might look a bit complicated now for you, but try understanding dictionaries and how to structure your question and answers in them and you will see that your code then simplifies a lot, you just need to print the Question for the current state, read the answer and update the state according to the dictionary.
so you want something like
You maybe want a final state that gives you a victory, so you can get a special state that when reached will print "You won" and exit the game
Try to understand the logic of this program and you will find that a combination of state machines and dictionaries will make your code very flexible and very easy to understand.