1

I am working on a project where I must ask the user to make a selection, I must limit their answers between 1-4 and have the program inform them when they have made a unacceptable selection. My issue comes when I try to modify the limiter to not crash when the user enters a empty space as a input.

No matter how I alter this code below, I receive a invalid syntax error. Any suggestions on how to fix this error? I do not know if it has anything to do with the boolean not, but that is the part of the code I added which causes the error. The original limiter was given to the entire class by the instructor.

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.")

The exact error message.

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

2 Answers2

0

Final code at the end

So first of all, the indentation. If you don't indent the def and the while, Python Interpreter will understand that the while statement is outside the function definition. But because nothing is defined in the function, Python raise an IndentationError, suspecting something is going wrong.

EDIT so in fact, you have another issue : your if statement is on 2 lines. Python doesn't understand this, so in case it's an error, he will raise a SyntaxError. To avoid it, just put a backslash \ at the end of the first line of your if statement :

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

After that, if you enter an empty result in the input function, you'll get a SyntaxError. As told in What's the difference between raw_input() and input() in python3.x? Python2 will try to evaluate the input as Python code. If you want only a string, you can avoid this by using raw_input.

Last but not least, if you just press Enter key, you will get an IndexError. The reason is that you have an empty string "" and you try to get the first index of this string. There's two way to deal with it :

The first one is to change the order of your conditions, putting len(hairchoice) == 1 in the first position. The others wouldn't be evaluated if the first one is false.

The second one is to restrict the allowed strings. The if statement will looks like this : 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.")
Community
  • 1
  • 1
FunkySayu
  • 7,641
  • 10
  • 38
  • 61
  • Indentation wasn't an issue, that came about from pasting the code into this site. raw_input didn't fix the issue. – NotaGoodProgrammer Apr 26 '15 at 18:07
  • Damn i understand... In fact, it's because your condition is not in the same line. Add an \ at the end of the first line – FunkySayu Apr 26 '15 at 18:16
  • Ran into too many secondary problems with adjusting the code to use the original limiter. I am using the one you created as modified by Manjunath N. Thank you for the help you've both provided. – NotaGoodProgrammer Apr 26 '15 at 18:38
0

I Have improvised code from @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()

output:

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
-->
Manjunath N
  • 1,365
  • 11
  • 22