Python Forum
restrict user input to numerical values
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
restrict user input to numerical values
#1
while True:
    year = input('please enter a 4-digit year: ')

    digit = len(year)

    if digit != 4 and year != int:
        print("sorry, that was bad input")
    else:
        print(f"thanks ! your value is {year}")
        break
I want to only except 4 character inputs or display error with the prompt again, which is working but i also need input to be only numeric values. Len wont work on int so im stuck.
Yoriz write Apr-08-2023, 04:54 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply


Messages In This Thread
restrict user input to numerical values - by MCL169 - Apr-08-2023, 04:45 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  WHILE LOOP NOT RETURNING USER INPUT AFTER ZerroDivisionError! HELP! ayodele_martins1 7 1,212 Oct-01-2023, 07:36 PM
Last Post: ayodele_martins1
Question Inserting Numerical Value to the Element in Optionlist and Printing it into Entry drbilgehanbakirhan 1 877 Jan-30-2023, 05:16 AM
Last Post: deanhystad
  user input values into list of lists tauros73 3 1,170 Dec-29-2022, 05:54 PM
Last Post: deanhystad
Information How to take url in telegram bot user input and put it as an argument in a function? askfriends 0 1,210 Dec-25-2022, 03:00 PM
Last Post: askfriends
Question Take user input and split files using 7z in python askfriends 2 1,200 Dec-11-2022, 07:39 PM
Last Post: snippsat
Sad how to validate user input from database johnconar 3 2,080 Sep-11-2022, 12:36 PM
Last Post: ndc85430
  How to split the input taken from user into a single character? mHosseinDS86 3 1,272 Aug-17-2022, 12:43 PM
Last Post: Pedroski55
  Changing a string value to a numerical value using python code and a lamda function Led_Zeppelin 6 1,738 Jul-05-2022, 11:29 PM
Last Post: deanhystad
  Sorting numerical values provided by QAbstractTableModel BigMan 0 1,429 Jun-04-2022, 12:32 AM
Last Post: BigMan
  Use pexpect to send user input alisha17 0 2,026 May-10-2022, 02:44 AM
Last Post: alisha17

Forum Jump:

User Panel Messages

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