【问题标题】:how can I fix this code (username and password)while loop如何在循环中修复此代码(用户名和密码)
【发布时间】:2017-10-17 19:56:55
【问题描述】:
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")

此代码正在运行,但它一直说访问权限不断被授予

【问题讨论】:

  • 这段代码有很多错误,我什至不知道从哪里开始。
  • (1) 布尔表达式的评估方式与您的大脑评估英语语法的方式不同。 username and password == True 按字面意思计算为 username and (password == True) 而不是 (username == True) and (password == True)(2) 您需要使用= 而不是== 来分配变量值。 (3) Indentation matters in Python.
  • 您的脚本中有几个错误。你能告诉我,你在哪里检查用户名密码组合是否有效?

标签: python while-loop passwords


【解决方案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")

【讨论】:

    【解决方案2】:

    你可能想要这样的东西:

    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的哪一部分被执行。

    【讨论】:

    • 快速总结您对脚本所做的更改将大大改善答案:-)
    【解决方案3】:
    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
    

    【讨论】:

    • 您能否为您的答案添加一些解释?
    猜你喜欢
    • 2013-08-25
    • 2019-06-16
    • 1970-01-01
    • 2019-04-19
    • 2015-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多