Reputation: 103
I am new to Python and I am trying to make a script that gets a users choice to open a program like Windows Command Prompt. Since Windows Command Prompt is also opened with 'cmd' I want the user to have the ability to type both and get the same result.
I know I can put it in multiple elif statements, but I was wondering if I can just put the two (or more) in a list and have python check if what the user input is in the list, and if it is, open the program or does whatever else
here is some test code I have been working on for a bit and am completely stumped at this point:
userInput = input(">")
userList = []
userList.append(userInput)
commandPrompt = ["cmd", "command prompt"]
testList = ["test1", "test2"]
if userList in commandPrompt:
print("cmd worked")
elif userInput == testList:
print("testList worked")
else:
print("Did not work")
print(userList)
Sorry if this question has been asked before. I checked all over Google and Stack Overflow and was not able to find any article quite like what I was wanting to do or explain if it is or is not possible.
Upvotes: 0
Views: 76
Reputation: 24038
You can reduce your code to this:
userInput = input(">")
commandPrompt = ["cmd", "command prompt"]
testList = ["test1", "test2"]
if userInput in commandPrompt:
print("cmd worked")
elif userInput in testList:
print("testList worked")
else:
print("Did not work")
This will work just the way you want. You don't actually need the userList
for anything.
Upvotes: 0
Reputation: 362
Assuming I understand correctly, you're checking if userList
is in commandPrompt
. But commandPrompt
never contains a list so this won't ever be satisfied.
if userInput in commandPrompt:
feels like it might be what you need. You don't need to put the user's input into a list.
Upvotes: 1