+ 2

else statement

num = input() if num == 5: print("Number is 5") else: if num == 11: print("Number is 11") else: if num == 7: print("Number is 7") else: print("Number isn't 5, 11 or 7") What is wrong in this ?

31st Mar 2017, 3:16 PM
Tarun Tiwari
5 Respostas
+ 14
@Dayve There is... Python takes the user input and turns into a string... He will need to change num to integer... num = int(num)
31st Mar 2017, 10:01 PM
Gami
Gami - avatar
+ 8
Nothing wrong with it! It's working correct in CP. BTW you can use elif instead of else if.
31st Mar 2017, 3:22 PM
Dev
Dev - avatar
+ 5
The 'if num==5' statement will check if the variable num is equal to the number 5 (integer). And of course it never will, because you assigned a string to it (input by default acquires a string type). What you need to do is to change the first line to: num = int(input()) That shall do the trick ;)
31st Mar 2017, 8:29 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
0
But when I am giving any input its showing last satatement every time . Pls check once
31st Mar 2017, 6:24 PM
Tarun Tiwari
0
remove else: from all except last one
5th Apr 2017, 10:14 AM
Debagnik Sen
Debagnik Sen - avatar