2016-11-14 45 views
-4
user1 = "aqa code" 
pass1 = "uzair123" 
username = input("Enter username: ") 
password = input("Enter password: ") 
while username != user1: 
    print("Access denied: username is incorrect try again ") 
    username = input("Enter username again: ") 
    if username == user1: 
     print("access granted") 
     while password != pass1: 
      print("Password is still incorrect") 
      password=input("Enter password again: ") 
      if password == pass1: 
       print("Access granted") 
while password != pass1: 
    print("Access denied,Password is incorrect") 
    password=input("Enter password again: ") 
    if password == pass1: 
     print("Access granted") 

如何將.lower()添加到username/user1,以便在鍵入答案時不區分大小寫?請幫忙。python。如何添加.lower()。

+1

您可以簡單地使用'username.lower()使用raw_input() == user1'而不是'用戶名== user1' – ettanany

+0

的可能的複製[如何轉換字符串在Python中小寫?](http://stackoverflow.com/questions/6797984/how-to-convert-string-to-lowercase-in-python) – Nikaidoh

回答

0

您可以在輸入功能返回時調用.lower

username = input("Enter username: ").lower() 
password = input("Enter password: ").lower() 
+0

*調用,不通過。你不要在函數上調用它,而是在函數返回的字符串上調用它。 –

+0

嘗試過,但它不起作用 – user3820439

+0

奇數。這個對我有用。 – James

0

添加.lower()確實是你要尋找的解決方案,但您可能希望儘量簡化您的流了一點,所以你有較少的地方進行編輯,每當你想修改你輸入語句。爲此,我建議建立每個循環,最初是假的,只有得到用戶輸入一次每個循環中的變量:

loop_condition = False #initialize loop condition 
while not loop_condition: #loop until we say so 
    value = input("please enter a value: ") #human input 
    value = value.lower() #convert to lower case for comparison 
    if value == "password": #test 
     print("success") 
     loop_condition = True #loop will terminate when we hit our `while` again 
    else: 
     print("fail") 
print("rest of program goes here") 

在這裏你可以看到我們只叫input()一次,只有將它轉化成小寫在一個地方。簡單永遠是你的朋友。

注:如果您使用Python 2.7