我用Python对三个不同的用户进行了测试。我需要保存并显示登录用户参加测验的最后三个分数,我不知道怎么做
以下是我目前掌握的代码:
import sys
c2 = 0
Score = 0
CU1 = "Scott"
CU2 = "alexander"
CU3 = "Lisa1"
CP1 = "heyall"
CP2 = "password"
CP3 = "Simpson"
Questions = ["True or False? CPU stands for Central Processing Unit?", "True or False? On average magnetic tape is more expensive than an Optical disk.", "True or False? A Binary Search looks for items in an ordered list.", "True or False? Extended ASCII covers all major languages.", "True or False? Procedures always must take parameters.", "True or False? In flow charts input/output is represented in a diamond.", "True or False? The world's largest WAN is the cloud.", "True or False? POP3 is used to retrieve emails from a server.", "True or False? In hexidecimal the binary number 01001110 equals 4E.", "True or False? An interpreter is only required once to run the program."]
Answers = ["True", "False", "True", "False", "False", "False", "False", "True", "True", "False"]
uninput = input("Please type in your username to continue:")
if uninput == CU1 or CU2 or CU3:
Auth = True
else:
Auth = False
if Auth == False:
print("Username not found")
sys.exit("Username incorrect!")
if Auth == True:
print("Username found!")
psinput = input("Please type in your password to continue:")
if psinput == CP1 or CP2 or CP3:
Auth2 = True
else:
Auth2 = False
if Auth2 == False:
print("Password incorrect!")
sys.exit("Password incorrect")
if Auth2 == True:
import random
if Auth == True and Auth2 == True:
Count = 1
if CU1 == uninput and CP1 == psinput:
Count = Count + 1
elif CU2 == uninput and CP2 == psinput:
Count = Count + 1
elif CU3 == uninput and CP3 == psinput:
Count = Count + 1
else:
Count = 0
sys.exit("No access granted!")
if Count == 2:
print("Commencing Quiz")
while len(Questions) > 0:
Question = random.randint(0, (len(Questions))-1)
Ans = input(Questions[Question])
if Ans == Answers[Question]:
print("Correct!")
Questions.remove(Questions[Question])
Answers.remove(Answers[Question])
Score = Score + 1
c2 = c2 + 1
else:
print("Incorrect!")
Questions.remove(Questions[Question])
Score = Score
c2 = c2 + 1
if Score == 10:
print("Well done sport! You got", Score ,"I'm so proud of you!")
elif Score == 9:
print("Well done sport! You got", Score ,"I'm so proud of you!")
if Score == 8:
print("Well done sport! You got", Score ,"I'm so proud of you!")
elif Score == 7:
print("Good job you got", Score ,"! It's not so bad! I'm proud!")
elif Score == 6:
print("Try again. You only got", Score,"/10")
elif Score == 5:
print("Try again. You only got", Score,"/10")
elif Score == 4:
print("Try again. You only got", Score,"/10")
elif Score == 3:
print("Try again. You only got", Score,"/10")
elif Score == 2:
print("Try again. You only got", Score,"/10")
elif Score == 1:
print("Try again. You only got", Score,"/10")
elif Score == 0:
print("You're a disgrace! You only got", Score,"/10!!!")
如果有人想知道的话,uninput是用户名输入的缩写,而psinput是密码输入的缩写
我稍微修改了代码。输入_file.txt时使用的方法,该文件包含名称、分数和新分数,将附加到此文件中
输入文件:Input_file.txt
亚历山大| 6
代码
输出
要显示最后3个分数,您必须将分数保存在文件中,然后再读取它们
我会像这样将数据作为字典保存在JSON文件中
users.json
顺便说一句:类似的方式,我会把问题保存在JSON文件中
data.json
然后我就可以开始读了
测验后更新
并保存它
然后我可以使用
[-3:]
显示最后的3个分数(或更多)完整代码(包括其他更改)
我使用
random.shuffle(data)
来改变数据的顺序,然后我可以使用普通的for
-循环,代码更简单你的代码有几处错误,但既然这不是问题所在,我就不在这里讨论了
下次,试着让你的问题更简洁。你想具体完成什么?据我所知,你只是想按用户存储一些文本
如何使用Python存储内容
作为第一步,试着明确你想要存储什么。变量应该是什么样子? 对于这种情况,它可能类似于
lastScores = [6, 8, 9]
现在,我们要存储并加载它。您希望如何实际存储它
myquiz
文件中李>为了给您一些代码,我假设您希望将它作为每个用户的一个文件以纯文本的形式存储在同一个文件夹中
保存:
在这里,我们打开一个文件
C:/Path/to/quiz.folder/Scott_scores.txt
,用于w
编写由于它是一个文本文件,我们将分数转换为一行文本。lastScores的每个元素都转换为
str
,所有值都用逗号和空格连接起来。所以我们写的是6, 8, 9
最后,我们关闭该文件,以便其他进程稍后可以打开它
装载
没什么不同:
我们打开文件,这次是为了
r
阅读最后的分数是通过读取文件,在逗号+空格上拆分得到的。并且每个元素都被转换为
int
我们再次关闭文件
如果不确定该文件是否已存在,可以将其更改为:
相关问题 更多 >
编程相关推荐