0
Can somone tell me why the code always outputs "The pocket is red" no matter the value given?
#This program tells the user what color a certain pocket is try:#Trys to run the code pocketNum = 999#input("What number is your pocket? ") except Exception:#Runs this if an exception arises print "You must input a valid number!" else:#Otherwise it runs this code if (pocketNum == 0): print"The pocket is Green!" elif pocketNum == 1 or 3 or 5 or 7 or 9 or 12 or 14 or 16 or 18 or 19 or 21 or 23 or 25 or 27 or 30 or 32 or 34 or 36: print "The pocket is Red!" elif pocketNum == 2 or 4 or 6 or 8 or 10 or 11 or 13 or 15 or 17 or 20 or 22 or 24 or 26 or 28 or 29 or 31 or 33 or 35: print"The pocket is Black!" else: print "You must input a number betwwen 1-36!"
7 Réponses
+ 1
You have spaces between or statements like 12 or 13... note the double space, space is 1, it returns true, so you have more true results under red pocket section. To be exact you have 5 extra "white" spaces in the red elif.
+ 1
when you input a string, you have to either convert it to an int or acknowledge it as a string in your if statement..
pocketNum=input("What is your pocket") .. let's say 1
if pocketNum == 1 .. this returns false
if pocketNum == "1" .. this returns true
+ 1
@ Justin Hill that's another good catch. I didn't even notice that since it was commented out. I edited the code in my answer to reflect this. ;-)
Surround your input with int() to convert it to an integer like:
int(input("What number is your pocket? "))
Note that the except clause won't catch anything as long as something of any type is input successfully. So a letter or number will be returned as a string and pass over this clause just fine going to the if elif else statement where it can potentially crash. If you wrap it in int() it should catch anything that cannot be parsed as an int and should work the way you would expect.
0
Holy or statements!!! lol
Your or statements are being used incorrectly.
pocketNum == 1 or 3 or 5 or 7 etc isn't valid.
You could do something like:
pocketNum == 1 or pocketNum == 3 or pocketNum == 5 etc
but it would be easier to use:
if pocketNum in (1, 3, 5, 7, ....):
# This program tells the user what color a certain pocket is
try: # Tries to run the code
pocketNum = int(input("What number is your pocket? "))
except Exception: # Runs this if an exception arises
print("You must input a valid number!")
else: # Otherwise it runs this code
if pocketNum == 0:
print("The pocket is Green!")
elif pocketNum in (1, 3, 5, 7, 9, 12, 14, 16, 18, 19, 21, 23, 25, 27, 30, 32, 34, 36):
print("The pocket is Red!")
elif pocketNum in (2, 4, 6, 8, 10, 11, 13, 15, 17, 20, 22, 24, 26, 28, 29, 31, 33, 35):
print("The pocket is Black!")
else:
print("You must input a number between 1-36!")
you could also do something like this:
elif (11 > pocketNum > 0 != (pocketNum % 2)) or ((37 > pocketNum > 11) and (pocketNum % 2 == 0)):
in place of:
elif pocketNum in (1, 3, 5, 7, 9, 12, 14, 16, 18, 19, 21, 23, 25, 27, 30, 32, 34, 36):
and then something similar for the other elif statement.
elif (12 > pocketNum > 0 == (pocketNum % 2)) or ((36 > pocketNum > 12) and (pocketNum % 2 != 0)):
0
This is python 2.7 btw
0
Thank you everyone
0
@ Paul Silvestri
The answers are valid for both 2.7 and 3.5 (I was using 3.5)
The only real difference is that you could omit the () Parentheses for the print statement if you wanted.