0

It's for my school work. I need to fix this code. Can someone maybe help me? I would really appreciate that. I can't manage to fix the "else" part. :)

CorrectUsername = "Username"
CorrectPassword = "Password"

loop = 'true'
while (loop == 'true'):
username = raw_input("Please enter your username: ")
if (username == CorrectUsername):
password = raw_input("Please enter your password: ")
if (password == CorrectPassword):
print "Logged in successfully as " + username
loop = 'false'
else:
print "Password incorrect!"
else:
print "Username incorrect!"

This is what I've done so far

CorrectUsername = "Username"
CorrectPassword = "Password"

loop = "true"
while (loop == 'true'):
    username = input("Please enter your username: ")
    if (username == CorrectUsername):
        password = input("Please enter your password: ")
        if (password == CorrectPassword):
            print("Logged successfully as " + str(username))
            loop = "false"
    else:
        print("Password incorrect!")
        else:
            print("Username incorrect")

3
  • Hi, welcome to StackOverflow! What are you trying to do with the else block? Commented Apr 22, 2021 at 10:20
  • Why are using strings instead of booleans? Commented Apr 22, 2021 at 10:21
  • 1
    Hi, welcome to StackOverflow ! You should take the Tour on StackOverflow, explaining how to properly ask a question. The community is here to help you, but you definitely need to provide more details to help us answer your question :) In your case, you should explain what is wrong with your code and what you are trying to achieve here :) Commented Apr 22, 2021 at 11:02

1 Answer 1

1

You have to indent the else properly it's matching if. Also use boolean values True/False, better than strings

CorrectUsername = "Username"
CorrectPassword = "Password"

loop = True
while loop:
    username = input("Please enter your username: ")
    if username == CorrectUsername:
        password = input("Please enter your password: ")
        if password == CorrectPassword:
            print("Logged successfully as " + str(username))
            loop = False
        else:
            print("Password incorrect!")
    else:
        print("Username incorrect")
Sign up to request clarification or add additional context in comments.

1 Comment

@RobertsKrūmiņš you can think about accepting the answer so (green tick onleft) ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.