Python Forum
if the input is not number, let user input again
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
if the input is not number, let user input again
#1
hi :) i am starting to learn python, and learning from Automating Boring Stuff With Python, then i tried to implement the knowledge and do something by myself.

i am creating this program that asks user for the age. when i tested the program, i realised if i didn't enter the number correctly as integer, the program would not continue.

so i tried to create a condition for the program to check whether it is integer or not, if not, the user can type again, and then the program would print 'thank you', and continue to print the calculated value, and continue until the end.

however, i am caught up for a lengthy period, so i think i should post a question to ask someone knowledgeable, here are my codes :

 
print('hello there')
print('what is your name?')

myName = input()
print('your name is ' + myName + ', it is nice to meet you')
print('the length of your name is composed of ' + str(len(myName)) + ' letters.')

print('what is your age?')
myAge = input(int())

if myAge == input(int()):
    print('thank you')
else:
    print('please enter the number only')

while myAge != int():
    print('please enter the number only')
    myAge = input(int())
print('thank you')

print('you will be ' + str(int(myAge) + 1) + ' next year')
print('this is the end of this program')
thank you :)
Reply


Messages In This Thread
if the input is not number, let user input again - by teatea - May-30-2020, 07:28 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Waiting for input from serial port, then move on KenHorse 3 1,072 Apr-17-2024, 07:21 AM
Last Post: DeaD_EyE
  Help with to check an Input list data with a data read from an external source sacharyya 3 410 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  difference between forms of input a list to function akbarza 6 1,036 Feb-21-2024, 08:02 PM
Last Post: bterwijn
  pyaudio seems to randomly halt input. elpidiovaldez5 2 378 Jan-22-2024, 09:07 AM
Last Post: elpidiovaldez5
  Receive Input on Same Line? johnywhy 8 727 Jan-16-2024, 03:45 AM
Last Post: johnywhy
  manually input data jpatierno 0 346 Nov-10-2023, 02:32 AM
Last Post: jpatierno
  Using string input for boolean tronic72 3 702 Nov-01-2023, 07:48 AM
Last Post: Gribouillis
  problem in using input command akbarza 4 1,134 Oct-19-2023, 03:27 PM
Last Post: popejose
  problem in entering address of a file in input akbarza 0 654 Oct-18-2023, 08:16 AM
Last Post: akbarza
  Input network device connection info from data file edroche3rd 6 1,048 Oct-12-2023, 02:18 AM
Last Post: edroche3rd

Forum Jump:

User Panel Messages

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