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
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
@pyzyx3qwerty sorry i will not do it again.. :)

Possibly Related Threads…
Thread Author Replies Views Last Post
  Exit function from nested function based on user input Turtle 5 324 Oct-10-2021, 12:55 AM
Last Post: Turtle
Star I'm getting syntax error while using input function in def. yecktmpmbyrv 1 195 Oct-06-2021, 09:39 AM
Last Post: menator01
  How to include input as part of variable name Mark17 4 409 Oct-01-2021, 06:45 PM
Last Post: Mark17
Exclamation question about input, while loop, then print jamie_01 5 385 Sep-30-2021, 12:46 PM
Last Post: Underscore
  Found input variables with inconsistent numbers of samples: [1000, 200] jenya56 2 229 Sep-15-2021, 12:48 PM
Last Post: jenya56
  [SOLVED] Input parameter: Single file or glob? Winfried 0 209 Sep-10-2021, 11:54 AM
Last Post: Winfried
  input data validation plumberpy 2 410 Aug-11-2021, 12:04 PM
Last Post: plumberpy
Big Grin Newbie to Python - input and branching issue Sherine 3 670 Jul-31-2021, 01:09 AM
Last Post: Pedroski55
  Unable to access the user input value given to daterangepicker klllmmm 1 704 May-26-2021, 09:16 PM
Last Post: nilamo
  [SOLVED] Good way to handle input args? Winfried 2 541 May-18-2021, 07:33 PM
Last Post: Winfried

Forum Jump:

User Panel Messages

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