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
(May-31-2020, 03:17 PM)ibutun Wrote: this programme work perfect but when i enter my name like 15.1 (float) is says:
What is your name: 15.1 Your name is 15.1. It's nice to meet you The length of your name is 4 letters.
is this float is not a digit ?

The isdigit() method only returns True if every character in the string is a digit. The '.' in a float value is not a digit, so isdigit() returns False for a float. You can use isalpha() to make sure all of the characters are letters, or you could use isalpha() and istitle() together to ensure the first letter is capitalized. (You could also just use istitle() in your print statement like menator01 does.)

while True:
    my_name = input('What is your name? ')
    if not my_name.isalpha():
        print('Please use only letters for your name.')
        print(f'Your name is {my_name}.')
        print('The length of your name is', len(my_name), 'characters.')

while True:
        my_age = int(input(f'What is your age, {my_name}? '))
        print('Your age is ' + str(my_age) + '.')
        print('You will be ' + str(my_age + 1) + ' in one year.')
    except ValueError:
        print('Please use only whole numbers for your age.')
thanks alot friends
Use try and except in the following manner.

Code to be ran if user entered int
Code to be ran if user did not enter integer.
@ibutun also remember, if you have a question, please do not post it in a thread start by another person - start your own thread. Please do that in the future
@pyzyx3qwerty sorry i will not do it again.. :)

Possibly Related Threads…
Thread Author Replies Views Last Post
  I want to check if the input is str or is int & if it's str repeat the loop HLD202 4 126 Nov-23-2020, 11:01 PM
Last Post: perfringo
  input timer Nickd12 9 212 Nov-23-2020, 09:50 PM
Last Post: Nickd12
  Loop back through loop based on user input, keeping previous changes loop made? hbkpancakes 2 136 Nov-21-2020, 02:35 AM
Last Post: hbkpancakes
  Pyspark SQL Error - mismatched input 'FROM' expecting <EOF> Ariean 3 87 Nov-20-2020, 03:49 PM
Last Post: Ariean
  input timer Nickd12 0 120 Nov-18-2020, 12:31 AM
Last Post: Nickd12
  Handling multi-input/output audio in python bor1904 4 322 Nov-04-2020, 08:25 AM
Last Post: CHLOVRL
  calling a function and argument in an input phillup7 3 198 Oct-25-2020, 02:12 PM
Last Post: jefsummers
  Python win32api keybd_event: How do I input a string of characters? JaneTan 3 207 Oct-19-2020, 04:16 AM
Last Post: deanhystad
  Repeating lines of code by an input Josh_Albanos 3 382 Oct-15-2020, 01:04 AM
Last Post: deanhystad
  Grabing email from a class with input from name Nickd12 2 156 Oct-13-2020, 06:22 PM
Last Post: Nickd12

Forum Jump:

User Panel Messages

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