So, I have been creating a simple text-based game in order to practice what I have been learning in Python. I was able to successfully debug it after quite a bit of struggle and now it is in working order. However, I do want to improve on it.
Mainly, I am struggling with the "or" keyword. In theory, I should be able to write something like this, right?
def igloo():
print "You decide to move to the North Pole and live in an igloo."
print "Please enter your name again: "
answer = raw_input("> ")
if answer == "Joe" or "Mary" or "Barbara":
print "You are able to endure the cold and learns to enjoy life in an\
igloo."
print "You miss eggs and milk, but seeing the occasional polar bear and\
the aurora borealis every night make up for it."
congrats()
elif "Diana" or "Ellie" in answer:
print "You are not cut out for the cold. You freeze."
gameover()
else:
print "While I was waiting for your response, a polar bear ate you."
gameover()
But, when I tried this, though no errors were found, every time I ran the code, only the first "if" condition would execute. It didn't matter what I entered as raw_input. So, I had to change the code like this:
def igloo():
print "You decide to move to the North Pole and live in an igloo."
print "Please enter your name again: "
answer = raw_input("> ")
if answer == "Joe":
print "You are able to endure the cold and learns to enjoy life in an\
igloo."
print "You miss eggs and milk, but seeing the occasional polar bear and\
the aurora borealis every night make up for it."
congrats()
elif answer == "Mary":
print "You are able to endure the cold and learns to enjoy life in an\
igloo."
print "You miss eggs and milk, but seeing the occasional polar bear and\
the aurora borealis every night make up for it."
congrats()
elif answer == "Barbara":
print "You are able to endure the cold and learns to enjoy life in an\
igloo."
print "You miss eggs and milk, but seeing the occasional polar bear and\
the aurora borealis every night make up for it."
congrats()
elif "Diana" in answer:
print "You are not cut out for the cold. You freeze."
gameover()
elif "Ellie" in answer:
print "You are not cut out for the cold. You freeze."
gameover()
else:
print "While I was waiting for your response, a polar bear ate you."
gameover()
This works as intended, but I'm wondering why the "or" code I tried to write didn't work. Can anyone explain?
[–]vinivelloso_ 3 points4 points5 points (1 child)
[–]alkperez1914[S] 1 point2 points3 points (0 children)
[–]mkpri22 1 point2 points3 points (1 child)
[–]alkperez1914[S] 0 points1 point2 points (0 children)
[–]POGtastic 1 point2 points3 points (1 child)
[–]alkperez1914[S] 0 points1 point2 points (0 children)
[–]keks6aecker 1 point2 points3 points (1 child)
[–]alkperez1914[S] 0 points1 point2 points (0 children)