Matt Lee Matt Lee - 1 year ago 100
Python Question

Python: Loop Prompt Back Around

A lot of the errors of this Python file have been fixed but there is one last thing it's not working. I need the else statement to loop back to ask the question again if neither Yes or No is entered. You can probably see by the code what I was going for but I'm probably not even on the right track. Can someone help me with this one last thing?

#These are my variables which are just strings entered by the user.
friend = raw_input("Who is your friend? ")
my_name = raw_input("Enter Your Name: ")
game_system = raw_input("What's your favorite game system? ")
game_name = raw_input("What's your favorite game for that system? ")
game_status = raw_input("Do you have the game? (Yes or No) ")
game_store = raw_input("What is your favorite game store? ")
game_price = raw_input("What's the price of the game today? Enter a whole number. ")

#This is what is printed after all the prompts. There is no condition for this print.
print "I went outside today and my friend " + friend + " was outside waiting for me. He said \"" + my_name + ", did you get the new " + game_system + " game yet? You were getting " + game_name + " today, right?\""

#If the condition on the Yes or No question is yes, then this code runs.
if game_status == "YES":
print "\"You know I got it, man!\" I said. \"Awesome! Let's play it,\" he said. \"I heard " + game_name + " is supposed to be amazing!\" We went back inside and took turns playing until " + friend + " had to go home. Today was a fun day."

#If the condition is No, then this code runs.
elif game_status == "No":
print "\"Well let's go get it today and we can come back here and play it!\" We went down to " + game_store + " and baught " + game_name + " for $" + str(game_price) + " and we went back to my house to take turns playing until " + friend + " went home. Today was a good day. (Now try again with the No option!)"

#If the condition meets neither Yes or No, then this code runs, sending the user back to the same question again. This repeats until a condition is met.
raw_input("That answer didn't work. Try again? Do you have the game? (Yes or No) ")

Answer Source


if game_status: "YES"

isn't how you make an if statement. You're treating it like the syntax is

if some_variable: some_value

and if the variable has that value, the if statement triggers. In fact, the syntax is

if some_expression:

and if the expression evaluates to something considered true, the if statement triggers. When you want the if statement to trigger on game_status equalling "YES", the expression should be game_status == "YES", so the if line should go

if game_status == "YES":

Similarly, the elif line should go

elif game_status == "NO":
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download