Even once you fixed the mis-cased if and improper indentation in your code, it wouldn't work as you probably expected. To check a string against a set of strings, use in. Here's how you'd do it (and note that if is all lowercase and that the code within the if block is indented one level).
One approach:
if answer in ['y', 'Y', 'yes', 'Yes', 'YES']:
print("this will do the calculation")
Another:
if answer.lower() in ['y', 'yes']:
print("this will do the calculation")
Answer from Daniel DiPaolo on Stack OverflowEven once you fixed the mis-cased if and improper indentation in your code, it wouldn't work as you probably expected. To check a string against a set of strings, use in. Here's how you'd do it (and note that if is all lowercase and that the code within the if block is indented one level).
One approach:
if answer in ['y', 'Y', 'yes', 'Yes', 'YES']:
print("this will do the calculation")
Another:
if answer.lower() in ['y', 'yes']:
print("this will do the calculation")
If should be if. Your program should look like this:
answer = raw_input("Is the information correct? Enter Y for yes or N for no")
if answer.upper() == 'Y':
print("this will do the calculation")
else:
exit()
Note also that the indentation is important, because it marks a block in Python.
Comparing strings in if statements
Using or for multiple strings in an if statement
If statements in functions
Python: How can I use a String for a If-Statement? - Stack Overflow
I'm writing this through my phone, forgive me if the proper indentation is incorrect, as it displays differently in my screen, and auto correct might mess something up. I'm getting the following error: Name error = name 'yes' is not defined. I dug around, and this usually happens when the variable is not defined, or if the value is pulled before the variable being defined. I don't think the issue is with my logic, but moreso with the syntax. I even tried specifying that it was a string value, as that was a problem once, but it doesn't seem to work either. If I could get any help, I'd appreciate it.
Here's the code that's giving the error:
pizza = str(input("Do you like pizza? "))
If pizza == str(yes): print("User Likes Pizza") elif pizza == no: print("user does not like pizza") else print("Please Give Valid response")
**solved**
So I'm trying to use or in an if statement, a bit of pretext, I'm using pycharm to try and make a sort of text adventure game, and I want to allow for more than one version of the same thing for example "go east" or "east" But when I use or it just gives me all of the if statements at once and I'm not sure what to do
here's my code:
import random
health = random.randint(10, 38)
print("Note: please always put go in front of your direction")
while health > 0:
print("""You are in a flat plain with little memory of what you were doing.
To the north is a run down barn with what seems to be a living scarecrow.
To the south is a small river, you see some fish swimming around in it.
To the east is what seems to be a small forest.
To the west you only see more flatland"""
)
direction = input("what do you do? ").lower()
if direction == "go west" or "west":
print("""You head west, only finding more flatland.
Nothing interesting here, so you head back""")
if direction == "go south" or "south":
print("""You reach the river, the sound of the water calmly being splashed about by the fish sooths you.
you watch the fish for a while before heading back.""")
if direction == "go east" or "east":
print("""You head to the dark forrest, unable to see in front of you,
You hear a loud moan come from behind you, it's a zombie!
You try to run away but trip on a fallen branch,
just as you think it's the end you feel something grab you and lead you out of the forrest""")
if direction == "go north" or "north":
print("""As you approach the run down barn, you see the scarecrow plowing a field.
Nearby there are animal pens, overgrown with weeds and grass, it looks like nothing has been in them for a while.
As you reach the barn you see a run down house not to far away but before you can do anything the scarecrow pipes up.
'Well it's been a long time since I saw a friendly face aroun' these parts'
you explain what you remember to the scarecrow.
'I'll join you on your journey if you can find ma hat, it's somewhere in the forest over there'""")
help = input("Will you help the scarecrow? ")
if help == "yes":
print("""You enter the forest and begin to search around, you can go west back to the farm.
North or south around the edge of the forrest.
Or east, deeper into the forrest""")
direction2 = input("which way will you go? ")
if direction2 == "go east" or "east":
print("""As you head deeper into the small forrest it quickly becomes harder and harder to see.
After a minute its almost impossible to see in front of you.
You suddenly hear a loud moan coming from nearby.
'W-was that zombie?'""")
decision = input("will you run away? ")
if decision == "yes" or "run away":
print("""you run out of the forrest chased by the zombie to the treeline,
it appears it cant leave the forrest, the scarecrow rushes over upon seeing you bolt out of the forrest.
'well I'll be damned, that's ma hat, i'm sure you can take it out, so here take my sword'
the scarecrow hands you a sword, a little worse for ware but it'll still kill things""")
else:
print(f"""you pick up a stick from the ground,
flailing it around in front of you you manage to hit the zombie dealing {random.randint(0, 5)} damage
the zombie bites you dealing {random.randint(2, 8)} """)
fight = input("what will you do? ")
if fight == "check health" or "health":
print (health)
else:
print("""You decide not to help the scarecrow by punching him in the face,
but before you can even move your hand the scarecrow impales the hoe he was using in your arm
'No one messes with sam the scarecrow, bitch!'
you lay there bleeding out, unable to move""")
breakto solve this problem u/newunit13 suggested I use in instead of or e.g
if direction in "go west" "west" "head west":
print("""You head west, only finding more flatland.
Nothing interesting here, so you head back""")
Use in operator
Because simple if better than complex.
inputs = [1, 2, 3, 4, 5, 6, 7]
value_filter = [4, 7]
for x in inputs:
if x in value_filter:
print(x, end=' ')
# 4 7
Use operator module
With the operator module, you can build a condition at runtime with a list of operator and values pairs to test the current value.
import operator
inputs = [1, 2, 3, 4, 5, 6, 7]
# This list can be dynamically changed if you need to
conditions = [
(operator.ge, 4), # value need to be greater or equal to 4
(operator.lt, 7), # value need to be lower than 7
]
for x in inputs:
# all to apply a and operator on all condition, use any for or
if all(condition(x, value) for condition, value in conditions):
print(x, end=' ')
# 4 5 6