关于文本游戏:文本游戏 – 如果语句基于输入文本 – Python

Text Game - If statement based of input text - Python

所以,我用python制作了一个基于文本的益智游戏,用于我的编程类(我们被迫使用python),所以我希望程序能够检测用户是否输入了"犹豫"或"行走"之类的内容,而不是让用户说1或2之类的简单内容。

目前我已经确定了用户输入中字符的数量,但是这使得他们几乎可以输入任何东西。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#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.
 ''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
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#

我如何才能更改它,这样,如果输入不是walk或犹豫,它就不会接受输入(在当前代码中,walk还没有包含在代码中)。我希望它是这样的;

1
2
3
4
5
6
7
if input ="Hesitate"
   print("You Hesitate")
else:
    if input ="Walk"
       print("You walk forward")
    else:
       print("Invalid Input")

我不知道如何在Python中正确地完成这项工作。我真的到处找过。


这是使用控制台吗?

1
2
3
4
5
6
7
8
9
10
11
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

这不是最好的方式去做这件事,但它很容易达到我插入你的要求。

事实上,答案在于你如何做这个循环。

在这个简单的方法中,它会一直运行,直到在得到有效的输入之后中断,一个更有效的解决方案可以通过使用make shift switch语句和一个字典找到,这里将讨论一个例子。或者,您可以在循环语句中使用更具体的条件。


基于另一个stackoverflow线程,我倾向于相信您会想要这样的东西:

1
2
3
4
5
6
7
if input =="Hesitate":
    print("You Hesitate")
else:
    if input =="Walk":
        print("You walk forward")
    else:
        print("Invalid Input")

我假设你已经得到了输入值。另外,如果给出这个答案,您需要强制资本化到预定的形式(第一个字母的资本都保持在较低的水平)。这条线可能有帮助。

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


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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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.
 ''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('')

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


您希望获取用户输入,然后使用while循环重新定义用户输入变量,直到获得有效的输入。所以像这样:

1
2
3
4
var = raw_input("Enter Text:")

while (var !="X" and var !="Y"):
    var = raw_input("Pick a different option:")

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