我有一种带有多个输入的文本冒险游戏:
import time
def menu():
print("This is the menu")
if input == 9:
menu()
print("Hello! Welcome to the world!")
heroname = str(input("What is your name?"))
heroname = heroname.lower()
print("Ah", heroname, "What an interesting name!!!")
sex = int(input("Now..are you a boy or a girl?(1 for boy, 2 for girl)"))
if sex == 1:
print("A male you are!")
herosex = 'sir'
if sex == 2:
print("Why greetings m'lady!")
herosex = 'mlady'
print("Please wait as we travel.")
time.sleep(1)
print(".")
time.sleep(1)
print(".")
print(herosex, ", we arrive!")
print("I wish to introduce you to the menu!")
print("1 - Okay!")
print("2 - I refuse!")
whatismenu = int(input("Enter your choice"))
if whatismenu == 1:
print("Ah, the menu is a dialogue that can be opened using 9 at any time beyond this point")
print("Here, try it!")
if whatismenu == 2:
print("You are quite rude, lets try this again. I would like to introduce you to the menu!")
print("1 - Very well, I am not trying to be a douchebag I just randomly become a dogboy")
whatismenu2 = int(input("Enter your choice"))
if whatismenu2 == 1:
print("Ah, the menu is a dialogue that can be opened using 9 at any time beyond this point")
我遇到的问题是我试图创建一种方法来打开第 4 行定义的菜单:
if input == 9:
基本上,通过所有这些不同的变量输入,如果用户在回答它们时按 9,它应该打开菜单功能,但我不知道如何在不测试每个输入变量的答案是否 == 9 的情况下执行此操作。如果有人对如何解决这个问题有任何建议,以及如何使该代码的方向对于文本冒险游戏更清晰有任何建议,我将不胜感激,我对Python真的很陌生。 谢谢。
最佳答案
我建议您制作自己的输入函数,我们只需将其命名为 myInput()
将所有 input() 替换为 myInput(),以便 myInput 自动检查它们是否输入了 9。
def myInput(prompt = ""):
inp = "9"
while inp == "9":
# Ask the user the same question until they enter something other than 9
inp = input(prompt)
if inp == "9":
menu()
return inp
我的 python 生锈了,所以你可能需要在这里改变一些东西
关于python - 基本的python,读取所有输入的全局输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35879628/