Reputation: 1
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
Upvotes: 0
Views: 59
Reputation: 1
You could try defining each question, then call question1() at the end to start it, like so:
def question1():
question1 = input("question1")
if question1 =="1":
print("Wrong")
elif question1 == "2":
print("Right, go on!!")
question2()
else:
print("Use a valid answer!")
question1()
def question2():
question2 = input("question2")
if question2 == "1":
print("Wrong")
question1()
elif question2 == "2":
print("Right, go on!!")
question3()
else:
print("Use a valid answer!")
question2()
def question3():
question3 = input("question3")
if question3 == "1":
print("wrong")
question1()
elif question3 == "2":
print("Right, go on!!")
else:
print("Use a valid answer!")
question3()
question1()
Upvotes: 0
Reputation:
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:
You are in question 1 and you have 3 possible answers:
1 -> Correct: Go to next question
2 -> Incorrect: Go back to first question
Anything else -> Repeat current question
Then you can mount each question with a nested dictionary structure, like this:
states = {
1: {
"Question": "The text of question 1",
"Answers": {
"1": 2, # Answer "1" will go to state 2
"2": 1, # Answer "2" will go to state 1
"Default": 1 # Anything else will stay in state 1
}
},
2: {
"Question": "The text of question 2",
"Answers": {
"2": 3, # Answer "2" will go to state 3
"1": 1, # Answer "1" will go to state 1
"Default": 2 # Anything else will stay in state 2
}
}
}
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
state = 1
You can then access the information of this state by doing
states[state]
For example the question of the current state is
states[state]["Question"]
And the answers dictionary are in
states[state]["Answers"]
So if the user inputs an answer answer
, you can get the appropriate response doing
states[state]["Answers"][answer]
This 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
state = 1
while True:
# print current question
print(states[state]["Question"])
# ask the user an answer
answer = input("What is your answer? ")
# check if answer exist for the current state
if answer in states[state]["Answers"].keys():
# if it exist go to the state pointed by that answer
state = states[state]["Answers"][answer]
else:
# if it doesn't exist, go to the state pointed by default
state = states[state]["Answers"]["Default"]
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.
Upvotes: 0