0
What am I doing wrong?
Okay, I am playing with the code below, but when I choose the number 12, the end result is "You lose." Shouldn't it be "You win." num = input("Choose a number") if num == 5: print("Number is 5") else: if num == 11: print("Number is 11") else: if num == 7: print("Number is 7") else: if num == 12: print("You win.") else: print ("You lose.")
6 Réponses
+ 2
Maybe you need to parse input to int.
However, please use elif statement instead of else if
+ 2
Try this:
num = input("Choose one, making you better feeling")
if num == 5:
print("No 5")
elif num == 11:
print("11 mofo")
elif num == 7:
print("Number 7")
elif num == 12:
print("Yay 12")
else:
print("Bug off")
The elif is a useful tool. Having nested elses is bad. Nests are bad in any language. TBH, the nesting makes your code illegible.
FYI: I'm not into Python, but that code should run.
+ 1
“11 mofo”😂😂😂 yeah it will run but it will display bug off every time if the user doesn’t input any of the given values so yes try avoiding else statements and replace them with else ifs
+ 1
you only lose if you dont smoke enough weed..
0
No because else is telling the program if num = 12 display you win and if the number is not twelve then display you lose
0
Thanks guys. I was working on the if else lesson, which has a bunch of nesting in it. Actually I just added a little to the already existing code in the test area. Most of it was already nested.