+ 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 RĂ©ponses
+ 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.