如何在循环时修复此代码(用户名和密码)

时间:2017-10-17 19:56:55

标签: python while-loop passwords username

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

此代码正在运行,但它一直表示持续授予访问权限

3 个答案:

答案 0 :(得分:1)

在语法和逻辑方面,代码中存在多个错误。在你走得更远之前,我建议你回去阅读基本的Python教程,直到你至少能够很好地理解语言的语法。

与此同时,这是您的代码的功能版本,经过大量评论,以便您可以跟进并查看正在发生的事情。

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

答案 1 :(得分:0)

你可能想要这样的东西:

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

如果您想再次提示凭据,可以在if "yes"内循环。

就像其他人所说的那样,开始时有一点“错误”。提示输入时,要将结果放入变量,只需使用=而不是== - 就像使用answer1

一样

为了便于理解,我保留了您的用户名/密码变量并为您的输入创建了新变量。一旦有人输入,则会检查这些值是否与chickenmonkeys匹配。结果决定if else的哪一部分被执行。

答案 2 :(得分:0)

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