0

line 8,9 specifically, Just started coding, so I do not know many functions nor syntax in Python. I just want any input provided by the user that isn't an integer (i.e, "I am _ years old") to be ignored. So it could then add one to the inputted integer.

print('hello, world')
print('what is your name?') #ask for their name
myName=input() #variable
print('It is good to meet you, '+myName)
print('the length of your name is:')
print(len(myName))
print('what is your age?') #ask for their age
myAge=input()
if not int(myAge) pass
print('you will be ' + str(int(myAge)+1) +' in a year')
narendra-choudhary
  • 4,582
  • 4
  • 38
  • 58

1 Answers1

0

Use try except to catch a ValueError. It is raised when the input could not be converted to an integer, e.g.

print('what is your age?')
try:
    myAge = int(input())
    print('you will be ' + str(int(myAge)+1) +' in a year')
except ValueError:
    pass
Erich
  • 1,838
  • 16
  • 20