Python选择利米

2024-05-15 03:53:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我在一个项目中,我必须要求用户作出选择,我必须限制他们的答案在1-4之间,并让程序通知他们,当他们作出了不可接受的选择。当我试图修改限制器,使其在用户输入空白空间时不会崩溃时,我的问题就出现了。你知道吗

不管我如何修改下面的代码,我都会收到一个无效的语法错误。关于如何修正这个错误有什么建议吗?我不知道它是否与boolean not有关,但这是我添加的代码中导致错误的部分。原来的限制器是老师发给全班的。你知道吗

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

Tags: and项目答案代码用户程序forif
2条回答

我有@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
 >

最后的代码

首先,压痕。如果不缩进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.")

相关问题 更多 >

    热门问题