Python 如何在循环时修复此代码(用户名和密码)
这段代码正在运行,但它一直在说,您可能需要这样的访问权限:Python 如何在循环时修复此代码(用户名和密码),python,while-loop,passwords,Python,While Loop,Passwords,这段代码正在运行,但它一直在说,您可能需要这样的访问权限: 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 ==
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所做的那样
为了便于理解,我留下了用户名/密码变量,并为您的输入创建了新变量。一旦有人输入了这两个值,就会检查它们的值是否匹配鸡
和猴子
。如果执行else
,则结果决定了执行的是的哪一部分。您的代码中有几个语法和逻辑错误。在继续之前,我建议您继续阅读基本的Python教程,直到您至少对该语言的语法有了很好的理解
同时,这里是代码的一个功能版本,有大量的注释,这样您就可以通过它查看发生了什么
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")`
这段代码有太多错误,我甚至不知道从哪里开始。(1)布尔表达式的计算方法与大脑计算英语语法的方法不同username和password==True
按字面意思计算为username和(password==True)
而不是(username==True)和(password==True)
。(2) 您需要使用=
而不是=
来分配变量值。(3) .你的剧本有几个错误。你能告诉我,你在哪里检查用户名密码组合是否有效吗?快速总结一下你对脚本所做的更改将大大改进答案:-)你能为你的答案添加一些解释吗?
# 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")
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