Text 文本游戏-基于If语句的输入文本-Python

Text 文本游戏-基于If语句的输入文本-Python,text,python-3.x,input,Text,Python 3.x,Input,因此,我正在为我的编程类使用Python制作一个基于文本的益智游戏(我们被迫使用Python),因此,我希望程序能够检测用户是否准确输入了“犹豫”或“行走”之类的简单内容,而不是让用户说1或2 目前我有它来确定用户输入的字符数量,但是这使得他们可以输入几乎任何东西 #Choice Number1 def introchoice(): print("Do you 'Hesitate? or do you 'Walk forward") def Hesitate():

因此,我正在为我的编程类使用Python制作一个基于文本的益智游戏(我们被迫使用Python),因此,我希望程序能够检测用户是否准确输入了“犹豫”或“行走”之类的简单内容,而不是让用户说1或2

目前我有它来确定用户输入的字符数量,但是这使得他们可以输入几乎任何东西

#Choice Number1
def introchoice():
    print("Do you 'Hesitate? or do you 'Walk forward")
        def Hesitate():
            print()
            print("You hesistate, startled by the sudden illumination of the room. Focusing on the old man who has his back turned to you. He gestures for you to come closer. \n ''Come in, Come in, don't be frightened. I'm but a frail old man'' he says.")
            print()
        #
        def Walk():
            print()
            print("DEFAULT")
            print()
        #Currently Determines Input
        InputVar = 5
        #User Input
        Choice = str(input())
        #Checks length
        if len(Choice) >= InputVar:
            Hesitate()
        else:
            if len(Choice) <= InputVar:
                Walk()
            else:
                print("Invalid Input")

#Intro Choice Def end
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
#Clean Up
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#

我不知道如何在python中正确地执行此操作。我诚实地到处搜索。

您希望获取用户输入,然后使用while循环重新定义用户输入变量,直到获得有效输入。比如说:

var = raw_input("Enter Text: ")

while (var != "X" and var != "Y"):
    var = raw_input("Pick a different option: ") 
if input == "Hesitate":
    print("You Hesitate")
else:
    if input == "Walk":
        print("You walk forward")
    else:
        print("Invalid Input")

编辑:raw_input()已更改为input()python 3.x

您希望获取用户输入,然后使用while循环重新定义用户输入变量,直到获得有效输入。比如说:

var = raw_input("Enter Text: ")

while (var != "X" and var != "Y"):
    var = raw_input("Pick a different option: ") 
if input == "Hesitate":
    print("You Hesitate")
else:
    if input == "Walk":
        print("You walk forward")
    else:
        print("Invalid Input")

编辑:原始输入()已更改为输入(),Python3.x

这是在使用控制台吗

while True:
    input = input("What do you do")
    if input == "Choice1":
        print("Consequence1")
        break
    if input == "Choice2":
        print("Consequence2")
        break
    if input == "Choice3":
        print("Consequence3")
        break
这不是做这件事的最佳方式,但它很容易达到我所说的你所要求的

实际上,答案在于你如何做循环


在这个简单的方法中,它不断地运行,直到在得到有效的输入后中断,使用makeshift-switch语句和字典可以找到更有效的解决方案,例如。或者,您可以在循环语句中使用更具体的条件。

这是在使用控制台吗

while True:
    input = input("What do you do")
    if input == "Choice1":
        print("Consequence1")
        break
    if input == "Choice2":
        print("Consequence2")
        break
    if input == "Choice3":
        print("Consequence3")
        break
这不是做这件事的最佳方式,但它很容易达到我所说的你所要求的

实际上,答案在于你如何做循环

在这个简单的方法中,它不断地运行,直到在得到有效的输入后中断,使用makeshift-switch语句和字典可以找到更有效的解决方案,例如。或者,您可以在循环语句中使用更具体的条件语句。

基于,我倾向于相信您希望这样:

var = raw_input("Enter Text: ")

while (var != "X" and var != "Y"):
    var = raw_input("Pick a different option: ") 
if input == "Hesitate":
    print("You Hesitate")
else:
    if input == "Walk":
        print("You walk forward")
    else:
        print("Invalid Input")
我假设你已经得到了输入值。同样,给出这个答案,你需要强制资本化到一个预定的形式(第一个字母大写所有剩余的更低)。这可能会有帮助

最后,回顾一下分配和平等之间的区别可能会有所帮助。在几种常见的编程语言中,可以使用“=”为变量赋值。在大多数相同的语言中,您会使用“==”来确定变量或常量的相等性。

基于此,我倾向于相信您会想要这样的结果:

var = raw_input("Enter Text: ")

while (var != "X" and var != "Y"):
    var = raw_input("Pick a different option: ") 
if input == "Hesitate":
    print("You Hesitate")
else:
    if input == "Walk":
        print("You walk forward")
    else:
        print("Invalid Input")
我假设你已经得到了输入值。同样,给出这个答案,你需要强制资本化到一个预定的形式(第一个字母大写所有剩余的更低)。这可能会有帮助


最后,回顾一下分配和平等之间的区别可能会有所帮助。在几种常见的编程语言中,可以使用“=”为变量赋值。在大多数相同的语言中,您会使用“==”来确定变量或常量的相等性。

至于公认的答案,将所有提示文本都放在
输入()函数中通常是不好的做法。另外,不要使用
len()
函数,这是不好的做法。你应该做的是:

def introchoice():
    #don't bother using three print statements every time.
    def printb(texttoprint):
        print()
        print(texttoprint)
        print()
    def Hesitate():
        printb('You hesistate, startled by the sudden illumination of the room. Focusing on the old man who has his back turned to you. He gestures for you to come closer. \n ''Come in, Come in, don't be frightened. I'm but a frail old man'' he says.')
    def Walk():
        printb('Default')
    input=null
    print('Do you Hesitate or Walk')
    input=input('')
    #Python3 is case sensitive, and you want to make sure to accept all cases
    while input.upper()!='HESITATE' and input.upper()!='WALK':
        print('Invalid input')
        input=input('')

此代码将执行您要求执行的操作。在确定输入值时不要检查输入的长度,应该循环直到得到有效的结果。

至于可接受的答案,将所有提示文本都放在
input()函数中通常是不好的做法。另外,不要使用
len()
函数,这是不好的做法。你应该做的是:

def introchoice():
    #don't bother using three print statements every time.
    def printb(texttoprint):
        print()
        print(texttoprint)
        print()
    def Hesitate():
        printb('You hesistate, startled by the sudden illumination of the room. Focusing on the old man who has his back turned to you. He gestures for you to come closer. \n ''Come in, Come in, don't be frightened. I'm but a frail old man'' he says.')
    def Walk():
        printb('Default')
    input=null
    print('Do you Hesitate or Walk')
    input=input('')
    #Python3 is case sensitive, and you want to make sure to accept all cases
    while input.upper()!='HESITATE' and input.upper()!='WALK':
        print('Invalid input')
        input=input('')

此代码将执行您要求执行的操作。在确定输入值时不要检查输入的长度,应该循环直到得到有效的内容。

我尝试了你的建议,但它不断返回语法Error@GameWylder,很抱歉,它的格式不完全是直接复制的,我补充道:“,现在应该可以解决问题了。我尝试了你的建议,但它不断返回一个语法Error@GameWylder,很抱歉,它的格式不完全可以直接复制,我添加了“:”,现在应该可以解决问题了。目前我的输入设置为:Choice=str(input())#这样行吗?你所说的似乎就是我所需要的。@GameWylder,我想你会想要一些类似于:Choice=input(“你是做什么的?”)的东西。另外,midfield99的答案显示了一个很好的输入方式。在许多情况下,最佳实践实际上是在等待输入时执行循环,以阻止您的程序(如果有意的话)。目前我的输入设置为:Choice=str(input())#这样行吗?你所说的似乎就是我所需要的。@GameWylder,我想你会想要一些类似于:Choice=input(“你是做什么的?”)的东西。另外,midfield99的答案显示了一个很好的输入方式。在许多场景中,最佳实践实际上是在等待输入时执行循环,以阻止您的程序(如果有意的话)。