2017-10-17 60 views
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": 
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") 

此代碼工作,但它口口聲聲說獲得連續授予我如何可以解決此代碼(用戶名和密碼),而環

+0

有這個代碼這麼多錯,我甚至不知道從哪裏開始。 – Verv

+0

**(1)**布爾表達式的評估方式與您的大腦評估英語語法的方式不同。 '用戶名和密碼== True'的字面意思是用戶名和(密碼== True)'不是'(用戶名== True)和(密碼== True)'。 **(2)**您需要使用'='而不是'=='分配變量值。 **(3)** [縮進在Python中很重要](https://stackoverflow.com/questions/45621722/im-getting-an-indentationerror-how-do-i-fix-it)。 –

+0

您的腳本中有幾處錯誤。你能告訴我,你在哪裏檢查,如果用戶名 - 密碼組合是有效的? –

回答

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

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

您可以在答案中添加一些解釋嗎? – Stein

+0

是的,我會添加評論的代碼 –

+0

大聲笑,比我能做得更好 –