0

如果有人有 python/jython 知识,我想更好地理解我的代码,并且愿意听。

这是我的代码(见下文)。我的结果是实现一个循环并保持程序运行,而不是每次用户输入错误的输入时重新启动程序。

该程序现在成功循环,但仅在输入正确输入后才显示错误消息,有人可以指出我正确的方向吗?提前致谢 :)

def inputValidator():

while True:
num = requestInteger("Please give me a number between 50 and 112") 

if num > 50 and num < 112:
 print "Successful Login" 
 break
elif num < 50: 
 print "Error! Please input a number more than 50 you entered", num
elif num > 112:
  print "Error! Please input a number less than 112 you entered", num
4

1 回答 1

1

So if I am understanding your question correctly, I assume you are just trying to get a user to input a number between 50 and 112?

It should look something like this, and indentation is very important!

def inputValidator():
    while True:
        try:
            number=int(raw_input("Enter a number between 50 and 112! >>> "))  ## Makes input an integer 
            if (number > 50) and (number < 112):   ## Checks if number is between 50 and 112
                print "Number accepted!"
                break
            else: 
                continue
        except:
            print "Please enter a number!"
    return number         ## Returns input number

Also, I am unsure if you know about "try" statements, but it just prevents the program from crashing. And you can also change around the text to make it look better! Hope this helps! :)

PS: I presume you just copied and pasted that code, so for future reference, use "Ctrl+K" to correctly and easily format it or just 4+ spaces! ;)

于 2015-01-13T06:20:56.283 回答