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

标签 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真的很陌生。 谢谢。

最佳答案

我建议您制作自己的输入函数,我们只需将其命名为 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/

相关文章:

python - 打开网页,然后单击背景中的坐标

Python 和 plot() : how to limit the number of x-axis ticks (labels)

html - 看不见的谷歌验证码

html - PowerShell:在输入类型=文件表单中写入值。

java - C++ 中的 scanf、memset 和几个 while 在 Java 中是什么意思?

javascript - 如何仅阻止页面上按键的特定默认操作?

python - 关闭 SQLAlchemy ORM session 是否会回滚未提交的更改?

python - for循环中不同大小数组的函数

python - 如何在多重继承中使用命名元组

input - netlogo 程序接受什么类型的输入