缩短字典if语句

  • 本文关键字:语句 if 字典 python
  • 更新时间 :
  • 英文 :


我想知道如何缩短if语句。现在这是一个很大的清单。但我想把它缩短。不幸的是,我已经在网上找了好几个小时了,但真的找不到解决方案。

有人能在我的代码中给我一个提示或示例吗?(对不起,我几天前才开始编程(

import time # for lockout after 5 attempts 
students = { # Make a static dictenoary called students
1: {"name": "Daniel","lastname": "maker","email" : "daniel@email.nl","password" : "123456"},
2: {"name": "Sandy","lastname": "Mango","email" : "sandy@email.nl","password" : "sdasdas!@"},
3: {"name": "Kreeft","lastname": "Garnaal","email" : "Kreeft@email.nl","password" : "Mekalekkea!@"},
4: {"name": "Alfred","lastname": "Skylake","email" : "Alfred@email.nl","password" :"suiker!@"}
}
count = 0 # Count the number of failed login attempts
while True: # Create a loop that requests and checks the credentials
print ("")
email = input("Enter your E-mail account: ")
passwords = input("Enter your password: ")
count += 1 #count the number of failed login attempts
print ("")

if (email == students[1]["email"] and passwords == students[1]["password"]): # Check credentials if it's correct on the right email and password.
print ("Welcome",students[1]["name"],students[1]["lastname"],"you are successfully logged in!")
break
elif(email == students[2]["email"] and passwords == students[2]["password"]):
print ("Welcome",students[2]["name"],students[2]["lastname"],"you are successfully logged in!")
break
elif (email == students[3]["email"] and passwords == students[3]["password"]):
print ("Welcome",students[3]["name"],students[3]["lastname"],"you are successfully logged in!")
break
elif (email == students[4]["email"] and passwords == students[4]["password"]):
print ("Welcome",students[4]["name"], students[4]["lastname"], "you are successfully logged in!")
break
elif count > 5:
tijd = time.localtime()
clock = time.strftime("%I:%M:%S %p", tijd)
print("On", clock, "You have logged incorrectly 5 times. You are blocked for 15 minutes")
#time.sleep(900) #Sleep 15 minutes.
break
else:
print ("Incorrect E-mail or password!")
print ("")

使用列表理解

selected_students = [s for s in students if s["email"] == email and s["password"] == password]

如果len(seleced_students(==0,那么您没有找到任何。

这不会破坏并始终贯穿完整的dict,但它比for更像蟒蛇,还会发现是否存在重复。

您可以用简单的for循环替换if-else语句:

import time 
students = { # Make a static dictenoary called students
1: {"name": "Daniel","lastname": "maker","email" : "daniel@email.nl","password" : "123456"},
2: {"name": "Sandy","lastname": "Mango","email" : "sandy@email.nl","password" : "sdasdas!@"},
3: {"name": "Kreeft","lastname": "Garnaal","email" : "Kreeft@email.nl","password" : "Mekalekkea!@"},
4: {"name": "Alfred","lastname": "Skylake","email" : "Alfred@email.nl","password" :"suiker!@"}
}

count = 0
while True:
email = input("nEnter your E-mail account: ")
passwords = input("Enter your password: ")
count += 1 #count the number of failed login attempts
login = False
print ("")

for student in students:
if students[student]["email"] == email:
if students[student]["password"] == passwords:
print (f"Welcome, {students[student]["name"]},{students[student]["lastname"]}, you are successfully logged in!")
count = 0
login = True
break
if not login:
print ("Incorrect E-mail or password!n")
if count > 5:
current_time = time.localtime()
clock = time.strftime("%I:%M:%S %p", current_time)
print("On", clock, "You have logged incorrectly 5 times. You are blocked for 15 minutes")
#time.sleep(900) #Sleep 15 minutes.
#count = 0 
break

您可以使用for循环,并使用for可以与else组合的Python功能,如果没有命中break语句,则执行else部分。

for i in students:
# Check credentials if it's correct on the right email and password.
if (email == students[i]["email"] and passwords == students[i]["password"]):
print("Welcome", students[i]["name"], students[i]["lastname"], "you are successfully logged in!")
break
elif count > 5:
tijd = time.localtime()
clock = time.strftime("%I:%M:%S %p", tijd)
print("On", clock, "You have logged incorrectly 5 times. You are blocked for 15 minutes")
# time.sleep(900) #Sleep 15 minutes.
break
else:
print("Incorrect E-mail or password!")
print("")

如果可以更改数据结构,您可能希望使用电子邮件作为字典的关键字,而不是数字。

students = {  # Make a static dictenoary called students
"daniel@email.nl": {"name": "Daniel", "lastname": "maker", "email": "daniel@email.nl", "password": "123456"},
"sandy@email.nl": {"name": "Sandy", "lastname": "Mango", "email": "sandy@email.nl", "password": "sdasdas!@"},
"Kreeft@email.nl": {"name": "Kreeft", "lastname": "Garnaal", "email": "Kreeft@email.nl", "password": "Mekalekkea!@"},
"Alfred@email.nl": {"name": "Alfred", "lastname": "Skylake", "email": "Alfred@email.nl", "password": "suiker!@"}
}
[...]
try:
student = students[email]
if passwords == student["password"]:
print("Welcome", student["name"], student["lastname"], "you are successfully logged in!")
break
elif count > 5:
tijd = time.localtime()
clock = time.strftime("%I:%M:%S %p", tijd)
print("On", clock, "You have logged incorrectly 5 times. You are blocked for 15 minutes")
# time.sleep(900) #Sleep 15 minutes.
break
else:
print("Incorrect E-mail or password!")
print("")
except KeyError:
print("Incorrect E-mail or password!")
print("")

您当然不想将密码存储在纯文本中,而是使用哈希。

最新更新