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
#11
(May-31-2020, 03:17 PM)ibutun Wrote: this programme work perfect but when i enter my name like 15.1 (float) is says:
Output:
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.')
        continue
    else:
        print(f'Your name is {my_name}.')
        print('The length of your name is', len(my_name), 'characters.')
        break

 
while True:
    try:
        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.')
        break
    except ValueError:
        print('Please use only whole numbers for your age.')
        continue
Reply
#12
thanks alot friends
Reply
#13
Use try and except in the following manner.

try:
Code to be ran if user entered int
except:
Code to be ran if user did not enter integer.
Reply
#14
@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
"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
Reply
#15
@pyzyx3qwerty sorry i will not do it again.. :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  interactive process for input and output maiya 1 442 Mar-27-2025, 08:40 AM
Last Post: Gribouillis
  Βad Input on line 12 Azdaghost 4 900 Mar-25-2025, 02:40 PM
Last Post: deanhystad
  How to revert back to a previous line from user input Sharkenn64u 2 793 Dec-28-2024, 08:02 AM
Last Post: Pedroski55
  How to make it so whatever I input into a script gets outputted on a different file spermatozwario 4 1,087 Nov-24-2024, 12:58 PM
Last Post: deanhystad
Question [SOLVED] Same input different output antarling 2 839 Oct-25-2024, 11:28 PM
Last Post: antarling
  I think I need to delete input data because returning to start fails thelad 2 1,043 Sep-24-2024, 10:12 AM
Last Post: thelad
  Input function oldschool 1 670 Sep-14-2024, 01:02 PM
Last Post: deanhystad
  User input with while loops chizzy101010 2 4,635 Aug-25-2024, 06:00 PM
Last Post: chizzy101010
  ValueError: could not broadcast input array from shape makingwithheld 1 2,120 Jul-06-2024, 03:02 PM
Last Post: paul18fr
  email address input jacksfrustration 5 2,199 Jun-26-2024, 08:35 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

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