Python选择限制器

时间:2015-04-26 17:41:29

标签: python select filter boolean limit

我正在开展一个项目,我必须要求用户做出选择,我必须将他们的答案限制在1-4之间,并让程序在做出不可接受的选择时通知他们。当我尝试将限制器修改为在用户输入空白空间作为输入时不崩溃时,我的问题出现了。

无论我如何更改下面的代码,我都会收到无效的语法错误。有关如何解决此错误的任何建议?我不知道它是否与布尔值有关,但这是我添加的代码中导致错误的部分。最初的限制器由教师给予整个班级。

def questionHairstyle():
    while True:
        hairchoice = ()
        questionsH = ("        1, for bald;" "\n" "        2, for crew-cut;"
        "\n" "        3, for curly;" "\n"   "        4, for wearing a hat;")
        print("Please enter a hairstyle:")
        print(questionsH)
        hairchoice = input("--> ", )
        print()
        if hairchoice[0] >= "1" and hairchoice[0] <= "4" 
        and not hairchoice[0] == " " and len(hairchoice) ==1:
        break
        print("Choice must be, between 1-4, not ", hairchoice + ".")
        print("Try again.")

确切的错误消息。

File "C:\Python34\Projects\sketch\sketch4.py", line 34
    if hairchoice[0] >= "1" and hairchoice[0] <= "4"
                                                    ^
SyntaxError: invalid syntax

2 个答案:

答案 0 :(得分:0)

最后的最终代码

首先,缩进。如果您不缩进defwhile,Python解释器会理解while语句 函数定义。但由于函数中没有定义任何内容,Python会引发IndentationError,怀疑出现了问题。

编辑所以事实上,您还有另一个问题:您的if声明有两行。 Python并不理解这一点,所以如果它出错,他会提出SyntaxError。要避免它,只需在if语句的第一行末尾添加反斜杠\

if hairchoice[0] >= "1" and hairchoice[0] <= "4" \
and not hairchoice[0] == " " and len(hairchoice) ==1:

之后,如果您在input函数中输入空结果,则会获得SyntaxError。正如What's the difference between raw_input() and input() in python3.x?中所述,Python2将尝试将输入评估为Python代码。如果您只想要一个字符串,可以使用raw_input来避免这种情况。

最后但并非最不重要的是,如果您只需按Enter键,您将获得IndexError。原因是你有一个空字符串"",你试图得到这个字符串的第一个索引。有两种方法可以处理它:

第一个是改变条件的顺序,将len(hairchoice) == 1放在第一个位置。如果第一个是假的,那么其他人就不会被评估。

第二个是限制允许的字符串。 if语句如下所示:if hairchoice in ["1", "2", "3", "4"]:

def questionHairstyle():
    while True:
        hairchoice = ()
        questionsH = ("        1, for bald;" "\n" "        2, for crew-cut;"
        "\n" "        3, for curly;" "\n"   "        4, for wearing a hat;")
        print("Please enter a hairstyle:")
        print(questionsH)
        hairchoice = raw_input("--> ")
        print()
        if hairchoice in ["1", "2", "3", "4"]:
            break
        print("Choice must be, between 1-4, not ", hairchoice, ".")
        print("Try again.")

答案 1 :(得分:0)

我有来自@FunkySayu的即兴代码。

def questionHairstyle():
    while True:
        questionsH = ["    1, for bald;",
                "    2, for crew-cut;",
                "    3, for curly;",
                "    4, for wearing a hat"
                ]
        print("Please enter a hairstyle:")
        print("\n".join(questionsH))
        hairchoice = raw_input("-->")  #This will take even spaces
        print()
        if hairchoice in ["1", "2", "3", "4"]:
            break
        elif hairchoice == " ":     #This part checks for space as input from user
            print ("Space is encountered ")
            print("Choice must be, between 1-4, not ", hairchoice, ".")
            # This part of code can be extended for other user input also
        print("Try again.")


questionHairstyle()

输出:

Please enter a hairstyle:
    1, for bald;
    2, for crew-cut;
    3, for curly;
    4, for wearing a hat
--> 
()
Space is encountered 
('Choice must be, between 1-4, not ', ' ', '.') #You can see the ' ' space is identified well in the output.
Try again.
Please enter a hairstyle:
    1, for bald;
    2, for crew-cut;
    3, for curly;
    4, for wearing a hat
-->