Reputation: 97
This code, more specifically in line 28, ask the users for multiple choices. The code does work, but theres one issue and I cant find an answer that adapts to my case (this wont stop some of you from downvoting this question for some reason), anyways, the thing is that it keeps asking for input even tho I have told it to skip to second function (scenario_exit) which I didnt copy here to keep things simple. So thats the problem, it keeps asking me for input even tho I told the while to stop.
So, too long didnt read? Code loops at "You need to select one option", line 40.
scenario2options =['Approach exit door', 'Check for vital signs']
scenario2options_2 =['Shoot him', 'Let him live']
def scenario2(response):
print response
print "Conversation"
print "Conversation2"
print "Conversation3"
print scenario2options
decision_sc2 = 1
while decision_sc2 == 1:
decision_scenario2 = raw_input("> ")
if 'Approach exit door' in decision_scenario2:
print "Conversation"
decision_sc2 = 0
dead("Conversation")
elif 'Check for vital signs' in decision_scenario2:
print "One of them is still alive finish him off, says Lars"
print scenario2options_2
decision_sc2_2 = 1
while decision_sc2_2 == 1:
decision2_scenario2 = raw_input("> ")
if 'Shoot him' in decision2_scenario2:
decision_sc2_2 = 0
scenario2_exit("You deliver him a quick dead.")
elif 'Let him live' in decision2_scenario2:
decision_sc2_2 = 0
scenario2_exit("Conversation")
else:
print "You need to select one option"
decision_sc2_2 = 1
else:
print "You need to select one option"
Upvotes: 0
Views: 88
Reputation: 59113
In the 'Check for vital signs'
if
block you don't set decision_sc2
to anything different, so your outer loop will still repeat.
Consider a function like this to make this stuff easier:
def get_choice(options):
while True:
print options
choice = raw_input('> ').lower()
for i, opt in enumerate(options):
if opt.lower() in choice:
return i
print "You need to select one of the given options."
Then you can check:
if get_choice(scenario2options)==0: # first choice, i.e. Approach exit door
...
else: # other choice, i.e. Check for vital signs
...
without needing a succession of nested while loops.
Upvotes: 2
Reputation: 6395
Your outer while-loop will always only evaluate the else-branch. Thus even if after the input decision_sc2_2 (you need to work on your variable naming....) is 0 (no need for the resetting it to 1 all the time in the last else-clause for the input), it will loop through the outer while again, reset decision_sc2_2 to 1, and the "game" continues.
Upvotes: 1