文本游戏 - 如果语句基于输入文本 - Python

时间:2014-04-07 16:24:19

标签: text python-3.x input

所以,我正在使用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
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#

如何改变这一点如果输入除了Walk或Hesitate之外什么都不会输入(在当前代码中,Walk还没有包含在代码中)。我希望它像是;

if input = "Hesitate"
   print("You Hesitate")
else:
    if input = "Walk"
       print("You walk forward")
    else:
       print("Invalid Input")

我无法弄清楚如何在python中正确执行此操作。老实说我到处搜寻。

4 个答案:

答案 0 :(得分:3)

这是使用控制台吗?

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语句可以找到更有效的解决方案,其中一个例子是discussed here。或者,您可以在循环语句中使用更具体的条件。

答案 1 :(得分:2)

基于another stackOverflow thread,我倾向于相信你想要这样的事情:

if input == "Hesitate":
    print("You Hesitate")
else:
    if input == "Walk":
        print("You walk forward")
    else:
        print("Invalid Input")

我在这里假设您已经获得了输入值。此外,根据这个答案,您需要强制大写到预定的形式(首字母大写全部保持较低)。 This thread可能对此有所帮助。

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

答案 2 :(得分:1)

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

var = raw_input("Enter Text: ")

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

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

答案 3 :(得分:1)

至于接受的答案,将所有提示文字放在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('')

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