What am I doing wrong? | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
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.")

25th Apr 2018, 9:57 PM
Drew A
Drew A - avatar
6 Respostas
+ 2
Maybe you need to parse input to int. However, please use elif statement instead of else if
25th Apr 2018, 10:00 PM
Bartosz Pieszko
Bartosz Pieszko - avatar
+ 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.
25th Apr 2018, 10:26 PM
non
+ 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
25th Apr 2018, 10:29 PM
<i>TheOfficialOdyssey</i>
<i>TheOfficialOdyssey</i> - avatar
+ 1
you only lose if you dont smoke enough weed..
26th Apr 2018, 1:59 AM
Tim Millar
Tim Millar - avatar
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
25th Apr 2018, 10:02 PM
<i>TheOfficialOdyssey</i>
<i>TheOfficialOdyssey</i> - avatar
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.
26th Apr 2018, 12:44 AM
Drew A
Drew A - avatar