PYTHON我做错了什么?循环和if语句

时间:2017-06-16 15:48:01

标签: python

我有这段代码:

methods = ["SNMP", "SUDP","ESSYN", "SSYN", "HTTP"]

print("Methods: {}".format(', '.join(methods)))
method = input("Enter method: ")
method = method.upper()

while method != methods:
    print("ERROR: Method unknown")
    method = input("Enter method: ")
    method = method.upper()

if method in methods:
    print("Method: {}".format(method))

print(""
      ""
      "")

seconds = input("Enter length in seconds: ")
    print("{} seconds".format(seconds))

你可以看到我试图从用户那里得到答案,然后显示答案并继续下一个任务。但如果答案不在方法列表中,我希望它再次提出问题。但我无法弄清楚如何。我现在使用的代码给出了错误消息"错误:方法未知"当它最终确实说:方法(用这里的方法)它不会进入下一个任务。任何人都可以告诉我该怎么做或这段代码有什么问题?

2 个答案:

答案 0 :(得分:1)

methods = ["SNMP", "SUDP","ESSYN", "SSYN", "HTTP"]

print("Methods: {}".format(', '.join(methods)))
ans = None
while ans is None: # when ans is set as method or any other value loop will stop asking for methods
    method = input("Enter method: ")
    if method.upper() in methods:
        ans = method  # when you set ans it will not ask again
        print("Method: {}".format(method))
        # rest of code here.... even another while loop for your input
    else:
        print("ERROR: Method unknown")

答案 1 :(得分:0)

怎么可能

while method != methods:

有意义吗?

也许你想要:

while method not in methods: