I am new to python programming. I want to call a function whenever the user enters the word "inventory"
after I prompt them for an input at any point in the game. At the moment I am doing this:
def myFunction():
#do something
userInput = input("Some input")
someMoreInput = input("Do something")
if userInput == "inventory" or someMoreInput == "inventory":
myFunction()
The problem with this is that whenver I want to have the user enter something (and that will happen a lot ) I have to add the variable name to my if statement.
You have to use a loop for this, for instance:
userInput = ''
while userInput != 'inventory':
userInput = input("Enter input: ")
myFunction()
As far as i have understood your question, you don't want to define a variable for your input again and again.
Well, you don't have to. You can compare directly:
if input("Some input")=="inventory":
myFunction()
Oscar's implementation seems to be correct however, if you want to check input every time without declaring a variable; simply use input()
def myFunction():
#do something
while input() != "inventory":
#print("you did not enter inventory")
input()
myFunction()
this will keep asking for input unless it equals "inventory" and you don't need to use even a single variable.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.