繁体   English   中英

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

[英]More efficient way to write this small bit of Python code?

我一直在阅读一本开头的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): ") 

无论如何,你拥有的功能都不正确。 考虑是否有人把“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

如果将代码包装在函数中,则可以避免使用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
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)
n = 0

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

print "ok!"

不使用异常处理的变体

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
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): ")

只有当not port.isdigit()为False - > port包含数字时, not port.isdigit() int(port)调用。 这是因为只有第一个为False才会计算or运算符的第二个操作数。

我试图避免代码重复,并使这个再现更加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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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