Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python input
#1
Hi guys i try to add elif where input is not a number to print 'please put number.' but for some reason its not working. Anyone know how to fix this? Wall

a = int(input())
b = int(input())



if b > a :
    print ('b is greater than a')
elif a == b:
    print('a and b are equal')
elif a > b :
    print('a is greater than b ')
elif a or b != int():
    print('please put number.')
Reply
#2
change
elif a or b != int():
    print('please put number.')
to
elif not isinstance(a, int) or not isinstance(b, int):
    print('please put number.')
Reply
#3
Still not working I got error
"
a = int(input())
ValueError: invalid literal for int() with base 10: 'f'
"
Reply
#4
Well yeah, "f" doesn't represent an integer in decimal. int throws an exception if it can't convert its argument (the exception is the ValueError you're seeing. You need to put that code within a try block and handle the exception in an except block.
Reply
#5
that's because you cannot convert a string to int, if not digits.

you can use for numeric input:
>>> while True:
...     a = input('Please enter a number: ')
...     try:
...         a = int(a)
...         break
...     except ValueError:
...         print("That's not a number, try again")
Reply
#6
Larz60+,ndc85430 thank You guys Im a begginer and sometimes its hard for me to understand simple errors , thank You anyway.
Reply


Forum Jump:

User Panel Messages

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