我正在为我的练习做一个像超速罚单程序的代码,我碰巧完成了我的大部分程序,但唯一的问题是,当我进入多个人并尝试打印摘要时,我希望从列表中显示他们的名字,他们加速的速度以及他们需要在单独的打印行中支付多少罚款但是因为' x'可以将多少人添加到列表中,我需要一些东西来说明所有这些都是分开的。这是代码:
login = ""
user = ["He-man", "Agent_awesome"]
wanted = ["JAMES WILSON", "HELGA NORMAN", "ZACHARY CONROY"]
speeders = []
password = ""
incorrect = 0
max_tries = 3
name = ""
total_fine = 0
fine = []
def security():
global login
global incorrect
while incorrect < max_tries:
login = input("""Welcome to the Speeding ticket program, in order to continue. Please enter the username (Note: Case-sensitive): """)
while True:
if login in user:
print("")
password()
else:
incorrect += 1
print("")
print("This username does not exist in the database, Please try again")
print("")
print("You now have {} chance(s) left".format(max_tries - incorrect))
print("")
security()
if incorrect == max_tries:
print("Sorry but have failed to login for 3 times thus you have been locked out")
raise SystemExit
def password():
global login
global incorrect
while incorrect <= max_tries:
password = input("Welcome {}, please enter the password (Note: Case-Sensitive): ".format(login))
while True:
if login == "He-man" and password == "Open sesame":
print("Welcome")
main()
elif login == "Agent_awesome" and password == "YOLO":
print("Welcome")
main()
else:
incorrect += 1
print("")
print("Sorry {} but that is the incorrect password".format(login))
print("Please try again")
print("")
print("You now have {} chance(s) left".format(max_tries - incorrect))
print("")
break
if incorrect == max_tries:
print("Sorry but have failed to login for 3 times thus you have been locked out")
raise SystemExit
def main():
global total_fine
global name
print("")
while True:
name = input("Please enter the name of the person who was speeding: ").upper()
if name in wanted: #This checks the wanted list to see whether anyone is in the wanted list and identifies them.
print("")
print("{} is WANTED and has a Warrant for Arrest".format(name))
raise SystemExit
else:
speeders.append(name)
overspeed_input()
def overspeed_input():
global name
global speed_overlimit
global speed_over
global speed_limit
print("")
speed_limit = int(input("What is the speed limit for that road: "))
print("")
speed_over = int(input("At what speed was {} travelling at: ".format(name)))
print("")
calculation()
def calculation():
global speed_overlimit
global speed_over
global speed_limit
global name
global total_fine
speed_overlimit = speed_over - speed_limit
if speed_overlimit < 10:
print("{} has to pay a fine of $30".format(name))
fine.append(30)
total_fine += 30
elif speed_overlimit >= 10 and speed_overlimit <= 14:
print("{} has to pay a fine of $80".format(name))
fine.append(80)
total_fine += 80
elif speed_overlimit >= 15 and speed_overlimit <= 19:
print("{} has to pay a fine of $120".format(name))
fine.append(120)
total_fine += 120
elif speed_overlimit >= 20 and speed_overlimit <= 24:
print("{} has to pay a fine of $170".format(name))
fine.append(170)
total_fine += 170
elif speed_overlimit >= 25 and speed_overlimit <= 29:
print("{} has to pay a fine of $230".format(name))
fine.append(230)
total_fine += 230
elif speed_overlimit >= 30 and speed_overlimit <= 34:
print("{} has to pay a fine of $300".format(name))
fine.append(300)
total_fine += 300
elif speed_overlimit >= 35 and speed_overlimit <=39:
print("{} has to pay a fine of $400".format(name))
fine.append(400)
total_fine += 400
elif speed_overlimit >= 40 and speed_overlimit <= 44:
print("{} has to pay a fine of $510".format(name))
fine.append(510)
total_fine += 510
elif speed_overlimit >= 45:
print("{} has to pay a fine of $630".format(name))
fine.append(630)
total_fine += 630
elif speed_overlimit <= 0:
print("Please input the correct speed!")
print("")
overspeed_input
retry()
def retry():
retry = input("Do you wish to enter another person (Y/N)?: ").upper()
while True:
if retry.upper() == "Y":
main()
elif retry.upper() == "N":
summary()
else:
print("Please enter using 'Y' or 'N' only!")
def summary():
print("Total Fines: {}".format(len(fine)))
????
raise SystemExit
security()
所以基本上,如果我进入,Jake Sorrow,speedoverlimit = 30和罚款= 300 然后我进入另一个像Mark kindley,speedoverlimit = 12和fine = 80的人 我希望输出如下:
Total Fines: 2
1) Name: Jake Sorrow | Speed overlimit = 30 | Fine = 300
2) Name: Mark Kindley | Speed overlimit = 12 | Fine = 80
我知道这是很多代码,我打算只提供最后一个代码,但它不能运行以前的功能,所以请耐心等待。
答案 0 :(得分:0)
您可以创建另一个列表,如
fines_list = []
running_total = 0
然后随便附上该列表,所以
running_total += 1
fineslist.append("{a}) Name: {b} | Speed Over Limit {c} | Fine = {d}".format(a = running_total, b = name, c = overlimit, d = fineamount)
然后使用列表推导或for循环
打印它[print(x) for x in fineslist]
for x in fineslist:
print(x)