简体   繁体   中英

python checking user input for number in a while loop

I have a function below which is part of my big main function, what I want this function to do is that whenever called, I want the function to check if the user input is a number or not. If it is a number it will return the number and break. But if it is not a number I want it to loop again and again.when I try to run it, it gives me unexpected an error:

unexpected eof while parsing 

can any body help me what I am missing or how I should rearrange my code? thank you!

def getnumber():
    keepgoing==True
    while keepgoing:
        number = input("input number: ") 
        result1 = number.isdigit()

        if result1 == 1:
            return number
            break
        elif keepgoing==True:

A neater and clearer what to do what you are already doing:

def getnumber():
    while True:
        number = input("Input number: ")
        if number.isdigit():
            return number

That's all you need, the extra variables are superfluous and the elif at the end makes no sense. You don't need to check booleans with == True or == 1 , you just do if condition: . And nothing happens after return , your break will never be reached.

You don't need the last line:

elif keepgoing==True:

It's waiting for the rest of the file after the : .

Side note, it should be a single = in the first part, and can just be written simpler as well.

def getnumber():
    while True:
        number = input("input number: ")
        result1 = number.isdigit()
        if result1:
            return number

Since you're inside the while loop, it'll keep executing. Using return will end the while loop, as will break ing and exiting the program. It will wait for input as well each time, though.

在分配您使用的keepgoing == True ,我认为应该为keepgoing=True

The following solution works on my machine, although I am running Python 2.7

def get_num():
    while True: #Loop forever
        number_str = raw_input("> Input a number: ") #Get the user input
        if number_str.isdigit(): #They entered a number!
            return int(number_str) #Cast the string to an integer and return it

I used raw_input rather than input , because raw_input gives you the exact string the user entered, rather than trying to evaluate the text, like input does. (If you pass the 12 to input , you'll get the number 12, but if you pass "12" , you'll get the string '12' . And, if you pass my_var , you'll get whatever value was in my_var ).

Anyway, you should also know that isdigit() returns whether or not the string has only digits in it and at least one character - that is not the same thing as isNumber() . For instance, "123".isdigit() is True, but "123.0".isdigit() is False. I also simplified your loop logic a bit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM