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
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.
+ 1
Thanks Jay, it works. But if I didnāt define it as a integer, then by default will it take as a string??
+ 1
Thanks.