Skip to content
← Back to Community
How to input numbers
Profile icon
CodeQueen4123

Since you guys are better than me do you mind telling me how to put a code for when I put in a command for it only to accept numbers

Answered by Highwayman [earned 5 cycles]
View Answer
Voters
Profile icon
MathiasOdegard
Profile icon
PAgravio
Profile icon
Shangyuan-Natha
Profile icon
DhanvineR
Profile icon
CodeQueen4123
Comments
hotnewtop
Profile icon
Highwayman

You can surround a casted input statement with a try - except block and a loop that just does another input if the cast fails.

Profile icon
CodeQueen4123

@Highwayman
Thank you I will try that

Profile icon
Highwayman
Profile icon
CodeQueen4123

@Highwayman
Hey I had to run an errand do you mind showing me what it should look like hope u r still on

Profile icon
CodeQueen4123

@Highwayman
Nvm I found out 🙏🏾 for ur help tho

Profile icon
Muffinlavania

You can als do this:

number = input("Enter a number") if number.isdigit():#If the input is a number: print("yay") else: print(":( i wanted a number")

Of course you can loop it and stuff but that is the foundation

Profile icon
thenullified
while True: s1 = input('number goes here') try: int(s1) except: print('pls enter a NUMBER') else: [*insert rest of code here*]
Profile icon
CodeQueen4123

Because it doesn’t say error when I don’t put numbers