import random
target_password = str(input("47983848:/n"))
password_length = len(target_password)
# Characters to create random passwords
characters = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0",\
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",\
"n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
# Flag to stop guessing when attempt is sucessfull
status = "ongoing"
def crack():
global target_password, status, password_length
# Number of guesses
count = 0
while status == "ongoing":
guess = ""
# Create a string from randomly chosen characters
while len(guess) < password_length:
# Add a random index from the characters list
guess += random.choice(characters)
# Compare guess to the real password
if guess == target_password:
print("Password cracked!: " + str(guess))
status = "finished"
count += 1
print(str(count) + " guesses")
else:
count+= 1
while password != "":
status = "ongoing"
crack()
print("________")
target_password = str(input("Enter another password you wish to test:\n"))
password_length = len(target_password)
it says that the line with else: count += 1 has invalid syntax can anyone help with the issue. this is for my stem fair
Hello @raisbello0516962928, welcome to the forums! As it’s currently indented, line 21 ( global …
) throws an indentation error, but I assume in the environment you’re coding in, it doesn’t? Can you please repost your code with indentation preserved?
@codeneutrino i’ve now edited the message with indentation. now the error should be the else statement
The error is because you have code between the if
statement and the else
statement that isn’t within the if
block. An else
block should follow directly from an if
block, meaning this works:
if condition:
code
else:
code
But this will throw an error:
if condition:
code
print("This line is not within the if block")
else:
code
Similarly, you cannot have an empty else
block, so this throws an error:
if condition:
code
else:
print("This line is not indented within the else block")
3 Likes
thank you for the assistance
2 Likes
but I just changed the code and removed the print, but it still says invalid syntax
Can you repost your code with all indentation preserved, and say what error message you’re getting?
I just reposted the code with indent preserved but its still providing the same error message
Did you edit your original message, because that’s throwing the same errors as before. Could you please post your new code in a new message?
import random
target_password = str(input("47983848:/n"))
password_length = len(target_password)
# Characters to create random passwords
characters = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0",\
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",\
"n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
# Flag to stop guessing when attempt is sucessful
status = "ongoing"
def crack():
global target_password, status, password_length
# Number of guesses
count = 0
while status == "ongoing":
guess = ""
# Create a string from randomly chosen characters
while len(guess) < password_length:
# Add a random index from the characters list
guess += random.choice(characters)
# Compare guess to the real password
if guess == target_password:
print("Password cracked!: " + str(guess))
status = "finished"
count += 1
else:
count+= 1
while password != "":
status = "ongoing"
crack()
print("________")
target_password = str(input("Enter another password you wish to test:\n"))
password_length = len(target_password)
still says “SyntaxError: invalid syntax” on line 26
That’s because you have code between your if
block and your else
block. This throws an error:
if condition:
pass
print("This line is not within the if block")
else:
pass
Notice how your if
block ends on line 22, and your else
block doesn’t start until line 25.
3 Likes
thank you. its working now.
1 Like