King Nish
King Nish

Reputation: 1

how can I fix this code (username and password)while loop

username = "chicken"
password = "monkeys"
print("Welcome to example")
answer1 = input ("Do you have an example account(Please type yes or no)?")
if answer1 == "yes":
username == input("please input username")
password == input("Please input your password")
while username and password == True:
    print("access granted")
        while username or password == False:
    print("access denied")

This code is working but it keeps saying access granted continuously

Upvotes: 0

Views: 3064

Answers (3)

Owen Cook
Owen Cook

Reputation: 43

answer1 = ''
user = ''
passw = ''
#these variables are set so that the while loops can work or there would be an error as the variables being checked in the while loop would not have been defined
print('Welcome to example')
while answer1 != 'yes' and answer1 != 'no':
# this makes it so that the program won't end if the user does not input either 'yes' or 'no'
    answer1 = str.lower(input('Do you have an example account(please enter "yes" or "no"'))
if answer1 == 'yes':
# if the user has the account then the program is able to run
    while user != 'chicken' and passw != 'monkey':
    # means that the program will keep going until the user inputs the right username and password
        user = str.lower(input('Username: '))
        # makes the input into lower case
        passw = str(input('Password: '))
        # makes sure that the variable is a string
        if user != 'chicken' or passw != 'monkeys':
        # if the user gets the username or password wrong
            print('Access Denied, try again.')
    # the while loop has now been broken so the user must have entered the correct username and password
    print('Access Granted')
elif answer1 == 'no':
# if the user doesn't have an account then they can't use the program
    print('OK then...')
  #good luck with the rest of the code for your gcse....
  # the original code was written by a friend

Upvotes: 0

Verv
Verv

Reputation: 2473

There are several mistakes in your code, both in terms of syntax and logic. Before you go further, I'd suggest you go back to reading basic Python tutorials, until you at least get a good understanding of the language's syntax.

Meanwhile, here's a functional version of your code, heavily commented so that you can follow through it and see what's going on.

# first we declare our valid username & password
username = "chicken"
password = "monkeys"

# we print our welcome message
print("Welcome to example")

# Then we ask our initial question
input_question = input("Do you have an example account (Please type yes or no)?")

# We only care if input was yes, otherwise
# program will terminate
if input_question == "yes":

    # then we initiate an infinite loop
    while True:

        # in each iteration, we prompt user to input
        # a username and password
        input_username = input("please input username")
        input_password = input("Please input your password")

        # and for each iteration, we check the input
        if input_username == username:
            if input_password == password:

                # if we reach this point, user has entered good
                # credentials, we print our success message and
                # break the loop
                print("access granted")
                break

        # if we reach this point, credentials didn't match
        # we print error message and let our loop reinitiate
        print("access denied")

Upvotes: 1

bhow
bhow

Reputation: 88

You probably want something like this:

username = "chicken"
password = "monkeys"
print("Welcome to example")
answer1 = input ("Do you have an example account(Please type yes or no)?")
if answer1 == "yes":
    user = input("please input username")
    passw = input("Please input your password")
    if username == user and password == passw:
        print("access granted")
    else:
        print("access denied")`

You can loop inside the if "yes" if you'd like to prompt for credentials again.

Like others have said, there was a decent bit "wrong" to begin with. When prompting for input, to put the result into a variable simply use = instead of == - like you did with answer1

To make it easier to understand, I left your username/password variables and created new variables for your input. Once someone has entered in both, they're checked if the values match chicken and monkeys. The result decides which part of the if else is executed.

Upvotes: 0

Related Questions