编写这一小段Python代码的更有效方法?

时间:2011-02-25 16:04:41

标签: python input

我一直在阅读一本开头的Python书,我一直在尝试编写一小段代码来接受用户输入,检查以确保它可以转换为int并检查它是否高于49152

我知道有一种更简单的方法可以做到这一点,但我无法理解这一点。

port_input = raw_input("port(number must be higher than 49152: ")

check = True
while check == True:
    check = False
    try:
        port_number = int(port_input)
    except:
        port_input = raw_input("port(number must be higher than 49152: ")
        check = True

while int(port_input) < 49152:
    port_input = raw_input("Please enter a higher number(hint: more than 49152): ") 

7 个答案:

答案 0 :(得分:3)

无论如何,你所拥有的功能都不正确。考虑是否有人把“123”然后“abc”。 123会让他们通过while check区块,但是当他们到达while < 49152区块时,则无法进行检查。

这是我想出的(我不做python,我只是根据你现有的代码入侵了它)。

check = True
while check :
    port_input = raw_input("port(number must be higher than 49152: ")
    try:
        port_number = int(port_input)
        check = (port_number < 49152)
    except ValueError:
        check = True

答案 1 :(得分:3)

如果将代码包装在函数中,则可以避免使用check标志:

def get_port():
    while True:
        port =  raw_input("port (number must be higher than 49152): ")
        try:
            port = int(port)
        except ValueError:
            continue
        if port > 49152:
            return port

答案 2 :(得分:1)

def get_input(msg = "port(number must be higher than 49152: "):
    port_input = raw_input(msg)
    try :
        if int(port_input) < 49152:
            return get_input("Please enter a higher number(hint: more than 49152): ")
    except ValueError:
        return get_input()
    return int(port_input)

答案 3 :(得分:1)

n = 0

while n < 49152:
    try:
        n=int(raw_input("enter number heghier than 49152->"))
    except: 
        print "not integer!"

print "ok!"

答案 4 :(得分:1)

不使用异常处理的

变体

def portInput(text):
    portInput.port_value = 0
    while True:
        port_input = raw_input(text)
        if not port_input.isdigit(): yield "port must be numeric"
        else:
            portInput.port_value = int(port_input)
            if portInput.port_value <= 49152: yield "number must be higher than 49152"
            else: return

for error in portInput("port(number must be higher than 49152): "):
    print error

print "entered port: %d" % portInput.port_value

答案 5 :(得分:0)

port = raw_input("port(number must be higher than 49152): ")
while not port.isdigit() or int(port) <= 49152:
    print("port must be a number > 49152")
    port = input("port(number must be higher than 49152): ")

只有在int(port)为假时才会进行not port.isdigit()来电 - &gt;端口包含数字。这是因为or运算符的第二个操作数仅在第一个为False的情况下进行求值。

答案 6 :(得分:-1)

我试图避免代码重复,并使这个再现更加pythonic。请注意,我不是'除了:'而是专门捕获ValueError。通常人们会忘记'except:'也会捕获SyntaxError异常,这可能会导致搜索愚蠢的错字非常令人沮丧。我假设这里的端口号是TCP或UDP端口号,因此确保它也小于65536。

have_valid_input = False

while not have_valid_input:
    unsafe_port = raw_input('port: ')
    try:
        port_number = int(unsafe_port)
    except ValueError:
        pass
    else:
        if port_number > 49152 and port_number < 65536:
            have_valid_input = True

    if not have_valid_input:
        print 'Invalid port'

print 'port', port_number