Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I don't get this....
#1
Hello, everybody....I am new to this forum...And definitely very new in programming, ANYTHING....I have these few lines of code on the if, elif...just seven lines, I think...no matter what I do it evaluates to 'false' or 'sorry, you're not qualified to vote'. I've tried modifying the variables, to just one key, two values (age = 20,21) it still evaluates to false...somebody said there has to be a variable when getting input...I tried that...still no way...then somebody said that this is a python quirk, where python will only evaluate zero (0) as false, and everything else is evaluated to true...that just sounds a little off to me, and I don't wanna buy that .... yet.
I hope somebody from y'all can slap my head with a washing paddle and show me what I'm doing wrong....
Thanks a lot...
Here's the source code:

your_age= 20
your_age=21


input('please indicate your_age. ')

if 20:
    print('sorry. you are not yet qualified to vote.')
elif 21: 
    print ('good. please proceed to the voting booth.')
Whichever age I input, the answer comes back "sorry, you're not yet qualified to vote".
And if it's any help, I use both the 2.7 and 3.6 versions.

Thanks again.

Pancit Bihon

PS : I've also tried this way, still no way...

your_age= 20
your_age=21


input('please indicate your_age. ')

if your_age <= 20:
print('sorry. you are not yet qualified to vote.')
elif your_age >=21:
print ('good. please proceed to the voting booth.')

This gives the 'good. please proceed...' response whichever age is indicated.
Reply
#2
Hello, next time please post code in Python code tags, this time I added them for you.

A lot is missing in the code, Python is smart but it cannot read your mind :)

As things are now, your input gets "lost" after you enter age. Instead you need to store it in a variable (e.g. "age"):
age = int(input('please indicate your_age. '))
If statement will return True or False, based on result of the evaluation. if 20: Will always evaluate to True, that is why you always get same result. Instead you need something like this:
if age <= 20:
    print('sorry. you are not yet qualified to vote.')
elif age > 20: 
    print ('good. please proceed to the voting booth.')
Take some time to read the docs on IF and other Python statements.

Stick with Python 3, Python 2 support is ending), Python 3 is the future.

Edit:

In the additional code you posted, your_age is first assigned 20 and then 21, and remains so for the rest of the program. In this case the elif statement evaluates to True (your_age is 21, which is greater or equal than 21), so you always get same result. Again, whatever input you give it doesn't get stored anywhere.

And another thing, input returns you a string, so if you want to compare the integer values, you need to convert it with int().

Edit2:

Quote:there has to be a variable when getting input..........a python quirk, where python will only evaluate zero (0) as false, and everything else is evaluated to true
These are correct.
Reply
#3
Moderator,

Thank you for the answer....it greatly helped.

pancit
Reply
#4
Zero, None, an empty list, dict or whatever, an empty string evaluates to False.

In [1]: bool(0)
Out[1]: False

In [2]: bool([])
Out[2]: False

In [3]: bool(None)
Out[3]: False

In [4]: bool("")
Out[4]: False
Everything else evaluates to True. For example 20.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020