elif statement issue | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 1

elif statement issue

Hi all, I m new to python. I was just tried the following code for my practice but it seems I m geeting wrong answer. Please let me know where Iā€™m making mistake. >>> a=input(ā€œEnter a number between 1 to 4: ā€œ) Enter a number between 1 to 4: 2 >>> if a==1: Print(ā€œEntered number is 1ā€) elif a==2: Print (ā€œEntered number is 2ā€) elif a==3: Print (ā€œEntered number is 3ā€) else: Print(ā€œEntered number is 4ā€) Output : Entered number is 4 How?? I entered 2

2nd Jun 2019, 6:11 AM
Baskar
3 Respostas
+ 6
This is a very common issue that happens frequently. If you use input() function and enter something, this will always be stored as string. So in your case input may be 2 but itā€˜s stored as ā€˜2ā€™. So you either have to compare it as strings like : if a == ā€˜2ā€™ or you convert input instanty to int as Jay Matthews mentioned.
2nd Jun 2019, 6:22 AM
Lothar
Lothar - avatar
+ 1
Thanks Jay, it works. But if I didnā€™t define it as a integer, then by default will it take as a string??
2nd Jun 2019, 6:18 AM
Baskar
+ 1
Thanks.
2nd Jun 2019, 6:23 AM
Baskar