Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unexpected syntax error
#1
I'm using the following code:

if score == 1 or score == 2: # if a weak password is created asks the user to try again
        print("This is a weak password, please try again")
        password = input("Please enter the new password:")
elif score == 3 or score == 4: # if a mediocre password is given asks the user whether they want to try again
       retry =  input("This is a weak password, do you want to try again? (y/n)")                             
if retry == "y": # if yes they restart the loop
           password = input("Please enter the new password:")
else retry == "n": # if no the loop is closed 
            try_again = False
else score == 5: # if strong password is given closes the loop 
        print("That is a strong password")
        try_again = False
return New_password
But I get this syntax error:

Error:
File "c:/Users/djwil/Documents/python/learning python/Chapter 19 - Chunky Challenges/Passwords.py", line 62 else retry == "n": # if no the loop is closed ^ SyntaxError: invalid syntax
But I'm not sure what's incorrect about my code. I can upload the full code if necessary the above is just a few lines of it.
Reply
#2
Why do you think it makes sense for else to have a condition associated with it? As in English, it means "in the other case", i.e. that none of the conditions for the preceding if or elifs are satisfied.
Reply
#3
You have a point, but I don't think that will solve my error as it occurs before that line of code.
Reply
#4
Just wondering, are there expected syntax errors?

Your indentation is all over the place. Fix that.

This is where I get a syntax error:
else retry == "n": # if no the loop is closed 
The reason is else can only be followed by :, not another condition. If you want another condition use elif as you did here:
elif score == 3 or score == 4:
From the Python documentation:

https://docs.python.org/3/reference/comp...-statement
8.1. The if statement
The if statement is used for conditional execution:

if_stmt ::=  "if" assignment_expression ":" suite
             ("elif" assignment_expression ":" suite)*
             ["else" ":" suite]
I hate to be a RTFM guy, but when you are learning a programming language it is a really good idea to read the manual. You don't have to read everything at once, but at least read it piecemeal. If you are having a problem at an "if ... else" go look at the documentation for if/else.

I also think it is a good idea to read the documentation in its entirety. Devote some of your "Python time" to reading a section from the language reference or reading about one of the standard libraries.
Reply
#5
So I've tried to address the indentation my code is now:
if score == 1 or score == 2: # if a weak password is created asks the user to try again
        print("This is a weak password, please try again")
        password = input("Please enter the new password:")
elif score == 3 or score == 4: # if a mediocre password is given asks the user whether they want to try again
        retry =  input("This is a weak password, do you want to try again? (y/n)")
        if retry == "y": # if yes they restart the loop
           password = input("Please enter the new password:")
        else: # if no the loop is closed 
            try_again = False
else: # if strong password is given closes the loop 
        print("That is a strong password")
        try_again = False
But I'm now getting an unexpected indent error:
Error:
File "c:/Users/djwil/Documents/python/learning python/Chapter 19 - Chunky Challenges/Passwords.py", line 120 if retry == "y": # if yes they restart the loop ^ IndentationError: unexpected indent
But I think that part of the code has to be indented to make sense
Reply
#6
I do not get a syntax error. If you want to know about a syntax error please provide the code that generates the error.

Is it possible that you are mixing tabs and spaces? I set up an editor specially for Python with 4 space indentation.
Reply
#7
I tried removing all indentations and then adding them in again using tab rather than the space bar.
The code is as follows:

def create_password(tmp):
    Num_list = ["1","2","3","4","5","6","7","8","9","0"] # creates list of numbers
    sc_list = ["!","£","$","%","^","&","*","(",")","@","?","#"] # creates list of special characters 
    password = input("Please enter the new password:")
    score = 0
    length = len(password)
    try_again = True
    Num = False
    sc = False
    upper = False
    lower = False
    while try_again == True:
        if length >= 8: # if password has more than 8 characters add 1 to the score 
            score = score + 1
    for x in password:
        if x.islower: # if a lower case character is included add  1 to the score
            lower = True
        if x.isupper: # same for upper case, numbers and special characters  
            upper = True
        if x in sc_list:
            sc = True
        if x in Num_list:
            Num = True
        if upper == True:
            score = score + 1 
        if lower == True:
            score = score + 1 
        if sc == True:
            score = score + 1
        if Num == True:
            score = score + 1 
    if score == 1 or score == 2: # if a weak password is created asks the user to try again
        print("This is a weak password, please try again")
        password = input("Please enter the new password:")
    elif score == 3 or score == 4: # if a mediocre password is given asks the user whether they want to try again
        retry =  input("This is a weak password, do you want to try again? (y/n)")
        if retry == "y": # if yes they restart the loop
            password = input("Please enter the new password:")
        else: # if no the loop is closed 
            try_again = False
    else: # if strong password is given closes the loop 
        print("That is a strong password")
        try_again = False
    return New_password
But I'm still getting the same error, and then I tried commenting out the lines of the elif score = 3 or 4 to check other parts of my code for errors then I get this other indentation error:

Error:
File "c:/Users/djwil/Documents/python/learning python/Chapter 19 - Chunky Challenges/Passwords.py", line 64 else: # if strong password is given closes the loop ^ IndentationError: expected an indented block
But that line is indented properly to my knowledge.
Reply
#8
No syntax error when I run your code. Some logic and programming errors.

The argument to the function is not used anywhere
def create_password(tmp):
To call a function you need to use parenthesis. The code below does not call isupper
x = 'a'
print(x.isupper)
Output:
<built-in method isupper of str object at 0x00000179EB55C370>
In Python indentation is not just to make things look pretty. Indentation is how code blocks are defined. In the code below the for loop is not inside the while loop.
    while try_again == True:
        if length >= 8: # if password has more than 8 characters add 1 to the score 
            score = score + 1
    for x in password:
The function returns a value that is never defined.
return New_password
I think the logic could use some work too. Doesn't it feel like there are a lot of steps in calculating the score? A small change like using 1/0 instead of True/False can make a big difference:
        upper = lower = digit = special = islong = 0
        if len(pwd) > 7:
            islong = 1
        for c in pwd:
            if c.isupper():
                upper = 1
            if c.islower():
                lower = 1
            if c in digits:
                digit = 1
            if c in specials:
                special = 1
        score = upper + lower + digit + special + islong
And "in" works with strings as well as lists.
    digits = "0123456789"
    specials = "!£$%^&*()@?#"
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Syntax error for "root = Tk()" dlwaddel 15 1,015 Jan-29-2024, 12:07 AM
Last Post: dlwaddel
Photo SYNTAX ERROR Yannko 3 335 Jan-19-2024, 01:20 PM
Last Post: rob101
  Syntax error while executing the Python code in Linux DivAsh 8 1,454 Jul-19-2023, 06:27 PM
Last Post: Lahearle
  Code is returning the incorrect values. syntax error 007sonic 6 1,137 Jun-19-2023, 03:35 AM
Last Post: 007sonic
  syntax error question - string mgallotti 5 1,251 Feb-03-2023, 05:10 PM
Last Post: mgallotti
  Syntax error? I don't see it KenHorse 4 1,194 Jan-15-2023, 07:49 PM
Last Post: Gribouillis
  Syntax error tibbj001 2 848 Dec-05-2022, 06:38 PM
Last Post: deanhystad
  Python-for-Android:p4a: syntax error in main.py while compiling apk jttolleson 2 1,778 Sep-17-2022, 04:09 AM
Last Post: jttolleson
  Mysql Syntax error in pymysql ilknurg 4 2,291 May-18-2022, 06:50 AM
Last Post: ibreeden
  Solving equation equal to zero: How to resolve the syntax error? alexfrol86 3 1,896 Feb-21-2022, 08:58 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020