Skip to content
Sign upLog in
← Back to Community

Odd or even finder doubt (Python 2.7)

Profile icon
Lanna

Screen Shot 2019-10-05 at 5.39.59 PM

Hiya folks!
What I'm trying to do in this programme is to find whether the number that the user has entered is odd or even.
While trying to do this, I have come across an obstacle that was brought to my attention by my coding teacher.
He asked me what would happen if the user enters a string or a float value. Ever since then, I'm trying to make this work but nothing seems to be happening.

Someone, please tell me what I'm doing wrong.
Thanks

Voters
Profile icon
albab992012
Profile icon
JamellButler
Profile icon
Lanna
Comments
hotnewtop
Profile icon
Geocube101

Line 3: (Get user input): You must convert it to a float or int using float() or int()
Line 4: (Test if number is whole number): Required only if you use float() on line 3

if number == int(number): print('This is a whole number') else: print('This is not a whole number')

Line 6, Line 13: (Testing even or odd): Change number to remainder (You're testing the wrong value)

Profile icon
MatthewDoan1

@Geocube101
I think int fails if the number is a string.

Profile icon
Geocube101

@MatthewDoan1
true, but you can loop the program until the string entered is numeric:

num = input('Enter a number: ') while num.isnumeric() == False: num = input('Invalid Number\nEnter a number: ') num = int(num) #Do stuff here