0

I have this code:

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

as you can see I'm trying to get an answer from the user then show the answer and go on to the next task. But if the answer is not on the list of methods, I want it to ask the question again. But I can't figure out how. The code that I use now is giving me the error message "ERROR: Method unknown" and when it finally does say: Method (with the method here) it won't go to the next task. Can anyone tell me what to do or what's wrong in this code?

FluffyMe
  • 105
  • 8

2 Answers2

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")
Gahan
  • 4,075
  • 4
  • 24
  • 44
  • Look at the linked duplicate - there's no need for initial values here and checking against it - proper use of `continue` or `break` are more than suitable. – Jon Clements Jun 16 '17 at 15:57
0

How could

while method != methods:

ever make sense?

Perhaps you want:

while method not in methods:
Michael Lorton
  • 43,060
  • 26
  • 103
  • 144