Absolute noob...patience, please


#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:

[python]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.’)[/python]

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.


#2

It’s a bit off, and it’s certainly not a quirk. Things that are empty or otherwise represent lack of something can be considered falsy ( '', (), [], {}, None, 0, 0.0, False, to mention a few)

No, but if you want to hold on to that later for later, then that’s what variables do

That doesn’t seem like a meaningful condition, perhaps you’d like to compare 20 to something first?

Comparing text to numbers isn’t meaningful either, and if you’re using python3 you’ll get an error message telling you as much. If you’re not using python3 then this would not “crash”, but then you should be using raw_input, not input, because input evaluates the input as if it were code, which is not a suitable method of converting text to a number (python3 renamed raw_input to input for this reason, and the old input did not get a new name (removed))


#3

This should work fine:

your_age = int(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.’)

I believe input statement treats the input value as a string and you have to convert it to an int if that is what you are expecting. Cheers!

  • Chan

#4

The indentation seems to have disappeared when posting.The print inside the if and elif need to be indented.


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.