基本的python,读取所有输入的全局输入?

时间:2016-03-08 22:51:40

标签: python input

我有一种带有多个输入的文字冒险游戏:

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的新手。 感谢。

1 个答案:

答案 0 :(得分:1)

我建议你自己创建输入函数让我们称之为myInput()

用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生锈了所以你可能需要在这里改变一些东西