Why is my python if statement not working?
Question:
I hope someone can help me. when I run the below function, no matter what the input, the rules are printed. I can’t see what I’ve done wrong.
def check_rules():
while True:
request = input("nWould you like to know the rules? (y/n) ")
if request == "y" or "Y":
print("""
1. Each player takes it in turn to roll a dice.
2. The player then turns over a card with the same
number as the number rolled to see how many ladybirds
there are (0-3).
3. The player keeps the card.
4. If a player rolls a number that is not on an unclaimed
card, play continues to the next player.
5. Play continues until there are no more cards.
6. The player with the most number of ladybirds wins.""")
break
elif request == "n" or "N":
break
else:
print("nI'm sorry, I didn't understand that.")
Answers:
Your if statement is not formed correctly:
def check_rules():
while True:
request = input("nWould you like to know the rules? (y/n) ")
if request in ["y","Y"]:
print("""
1. Each player takes it in turn to roll a dice.
2. The player then turns over a card with the same
number as the number rolled to see how many ladybirds
there are (0-3).
3. The player keeps the card.
4. If a player rolls a number that is not on an unclaimed
card, play continues to the next player.
5. Play continues until there are no more cards.
6. The player with the most number of ladybirds wins.""")
break
elif request in ["n","N"]:
break
else:
print("nI'm sorry, I didn't understand that.")
Boolean expressions cannot be like if something == x or y
, you must state them like if something == x or something == y
The if
statement doesn’t determine whether request equals y
or Y
. If determines the boolean value of request == "y"
which may be false. If it’s false it then determines the boolean value of "Y"
. Since a non empty string evaluates to True request == "y"
or "Y"
is always true.
I hope someone can help me. when I run the below function, no matter what the input, the rules are printed. I can’t see what I’ve done wrong.
def check_rules():
while True:
request = input("nWould you like to know the rules? (y/n) ")
if request == "y" or "Y":
print("""
1. Each player takes it in turn to roll a dice.
2. The player then turns over a card with the same
number as the number rolled to see how many ladybirds
there are (0-3).
3. The player keeps the card.
4. If a player rolls a number that is not on an unclaimed
card, play continues to the next player.
5. Play continues until there are no more cards.
6. The player with the most number of ladybirds wins.""")
break
elif request == "n" or "N":
break
else:
print("nI'm sorry, I didn't understand that.")
Your if statement is not formed correctly:
def check_rules():
while True:
request = input("nWould you like to know the rules? (y/n) ")
if request in ["y","Y"]:
print("""
1. Each player takes it in turn to roll a dice.
2. The player then turns over a card with the same
number as the number rolled to see how many ladybirds
there are (0-3).
3. The player keeps the card.
4. If a player rolls a number that is not on an unclaimed
card, play continues to the next player.
5. Play continues until there are no more cards.
6. The player with the most number of ladybirds wins.""")
break
elif request in ["n","N"]:
break
else:
print("nI'm sorry, I didn't understand that.")
Boolean expressions cannot be like if something == x or y
, you must state them like if something == x or something == y
The if
statement doesn’t determine whether request equals y
or Y
. If determines the boolean value of request == "y"
which may be false. If it’s false it then determines the boolean value of "Y"
. Since a non empty string evaluates to True request == "y"
or "Y"
is always true.