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
  Help with to check an Input list data with a data read from an external source sacharyya 3 317 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  difference between forms of input a list to function akbarza 6 928 Feb-21-2024, 08:02 PM
Last Post: bterwijn
  pyaudio seems to randomly halt input. elpidiovaldez5 2 311 Jan-22-2024, 09:07 AM
Last Post: elpidiovaldez5
  Receive Input on Same Line? johnywhy 8 607 Jan-16-2024, 03:45 AM
Last Post: johnywhy
  manually input data jpatierno 0 315 Nov-10-2023, 02:32 AM
Last Post: jpatierno
  Using string input for boolean tronic72 3 635 Nov-01-2023, 07:48 AM
Last Post: Gribouillis
  problem in using input command akbarza 4 996 Oct-19-2023, 03:27 PM
Last Post: popejose
  problem in entering address of a file in input akbarza 0 619 Oct-18-2023, 08:16 AM
Last Post: akbarza
  Waiting for input from serial port, then move on KenHorse 2 832 Oct-17-2023, 01:14 AM
Last Post: KenHorse
  Input network device connection info from data file edroche3rd 6 913 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