Reputation: 21
Okay, I'm coding a Pokemon text adventure kind of game and I need help with while loops. I've already created the while loops. But the part that's not working is: you have a choice between choosing two raw_inputs, run or battle. It won't show the message when you choose either one of those. All it does is repeat the question I programmed it to.
The question asks "Do you want to Run or Battle the Yveltal?" You can either type in "Run" or "Battle" in the iPython session. When you type Battle
, it's supposed to say "You challenged the Yveltal to a battle!" When you type Run
, it's supposed to say "You can't run you coward," but if you type anything in, all it does is ask the same question "Do you want to Run or Battle the Yveltal?"
What I want help with is leaving the while loop, and when you type either run or battle, it will show the message for that command. Here's the code, and I could use help from anyone, thank you!
from time import sleep
def start():
sleep(2)
print "Hello there, what is your name?"
name = raw_input ()
print "Oh.. So your name is %s!" % (name)
sleep(3)
print"\nWatch out %s a wild Yveltal appeared!" % (name)
sleep(4)
user_input = raw_input("Do you want to Run or Battle the Yveltal?" "\n")
while raw_input() != 'Battle' or user_input == 'battle' != 'Run' or user_input == 'run':
print("Do you want to Run or Battle the Yveltal? ")
if user_input == 'Battle' or user_input == 'battle':
print("You challenged Yveltal to a battle!")
elif user_input == 'Run' or user_input == 'run':
print("You can't run you coward!")
Upvotes: 2
Views: 814
Reputation: 555
Your while loop is riddled with errors or mistakes. Try this:
while user_input.lower() != "battle" or user_input.lower() != "run":
Using the .lower() function makes so that you don't have to plan for "RuN" or "baTTle". It converts the string to lower case so that you can just check the word. Also, you should use input() instead of raw_input() Honestly I would structure your code like this:
user_input = input("Run or battle?\n") #or whatever you want your question
user_input = user_input.lower()
while True:
if user_input == "battle":
print("You challenged Yveltal to a battle!")
break
elif user_input == "run":
print("You can't run you coward!")
user_input = input("Run or battle?\n")
user_input = user_input.lower()
break
else:
user_input = input("Run or battle?\n")
user_input = user_input.lower()
You might have better luck with code like that.
Upvotes: 1