How to make users only enter integer values in Python program

后端 未结 5 1356
别跟我提以往
别跟我提以往 2020-12-20 07:33

I have a homework assignment that I am very confused about. I have to write a program allowing users to input the number of eggs that they are buying. The program will then

5条回答
  •  情话喂你
    2020-12-20 07:48

    Let the user enter anything he wishes, and warn only if it wasn't an integer:

    try:
        num = int(input("enter number: "))
    except ValueError:
        print("you must enter an integer")
    

    This is the Pythonic way to do it, after all it's "Easier to ask forgiveness than permission".

提交回复
热议问题