Python Forum

Full Version: restrict user input to numerical values
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
Maybe you could use the strings isdigit method
if digit != 4  or not year.isdigit():
(Apr-08-2023, 05:00 PM)Yoriz Wrote: [ -> ]Maybe you could use the strings isdigit method
if digit != 4  or not year.isdigit():
That did the trick Thanks!