level 3
Hdkev1N
楼主
我想用python3编写一个从用户获取取学生id和学生姓名的程序。然后,程序会逐个显示 10 个问题,其中包含 4 个可能的答案(一个正确答案)。用户为每个问题选择
正确的
答案。最后,程序显示分数,用户 ID 和姓名。
问题应由程序从文件中读取。文件名叫 “Q.txt”
最后,把用户名和 ID 的分数将打印在另一个文件上。(R.txt)
def read_Q():
score=0
file=open('Q.txt','r')
flag=1
for line in file:
if flag==1:
print(file.readline(100))
print(file.readline(100))
print(file.readline(100))
print(file.readline(100))
print(file.readline(100))
flag=0
continue
if flag==0:
correct=line.strip()
answer=input("Enter your answer : ")
if(answer.upper()==correct):
score+=1
flag=1
continue
file.close()
return score
def write_score(score,name,uid):
file=open('R.txt','w')
file.write("Name : ")
file.write(name)
file.write('\n')
file.write("UID : ")
file.write(uid)
file.write('\n')
file.write("Score : ")
file.write(str(score))
file.close()
if __name__ == "__main__":
name=input("Enter name : ")
uid=input("Enter UID : ")
score=read_Q()
write_score(score,name,uid)
这是我写的,但是 他只能运行一道题目而且还不是从第一行开始的

这是Q.txt 的内容
Q1. what is the answer of 1+1
A. 1
B. 2
C. 3
D. 4
B
Q2. what is the answer of 1+2
A. 1
B. 2
C. 3
D. 4
C
Q3. what is the answer of 1
+3
A. 1
B. 2
C. 3
D. 4
D
Q4. what is the answer of 1+4
A. 5
B. 6
C. 7
D. 8
A
Q5. what is the answer of 1+5
A. 5
B. 6
C. 7
D. 8
B
Q6. what is the answer of 1+6
A. 5
B. 6
C. 7
D. 8
C
Q7. what is the answer of 1+7
A. 5
B. 6
C. 7
D. 8
D
Q8. what is the answer of 1+8
A. 9
B. 10
C. 11
D. 12
A
Q9. what is the answer of 1+9
A. 9
B. 10
C. 11
D. 12
B
Q10. what is the answer of 1+10
A. 9
B. 10
C. 11
D. 12
C
搞不懂了真的
2021年04月29日 23点04分
1
正确的
答案。最后,程序显示分数,用户 ID 和姓名。
问题应由程序从文件中读取。文件名叫 “Q.txt”
最后,把用户名和 ID 的分数将打印在另一个文件上。(R.txt)
def read_Q():
score=0
file=open('Q.txt','r')
flag=1
for line in file:
if flag==1:
print(file.readline(100))
print(file.readline(100))
print(file.readline(100))
print(file.readline(100))
print(file.readline(100))
flag=0
continue
if flag==0:
correct=line.strip()
answer=input("Enter your answer : ")
if(answer.upper()==correct):
score+=1
flag=1
continue
file.close()
return score
def write_score(score,name,uid):
file=open('R.txt','w')
file.write("Name : ")
file.write(name)
file.write('\n')
file.write("UID : ")
file.write(uid)
file.write('\n')
file.write("Score : ")
file.write(str(score))
file.close()
if __name__ == "__main__":
name=input("Enter name : ")
uid=input("Enter UID : ")
score=read_Q()
write_score(score,name,uid)
这是我写的,但是 他只能运行一道题目而且还不是从第一行开始的

这是Q.txt 的内容Q1. what is the answer of 1+1
A. 1
B. 2
C. 3
D. 4
B
Q2. what is the answer of 1+2
A. 1
B. 2
C. 3
D. 4
C
Q3. what is the answer of 1
+3
A. 1
B. 2
C. 3
D. 4
D
Q4. what is the answer of 1+4
A. 5
B. 6
C. 7
D. 8
A
Q5. what is the answer of 1+5
A. 5
B. 6
C. 7
D. 8
B
Q6. what is the answer of 1+6
A. 5
B. 6
C. 7
D. 8
C
Q7. what is the answer of 1+7
A. 5
B. 6
C. 7
D. 8
D
Q8. what is the answer of 1+8
A. 9
B. 10
C. 11
D. 12
A
Q9. what is the answer of 1+9
A. 9
B. 10
C. 11
D. 12
B
Q10. what is the answer of 1+10
A. 9
B. 10
C. 11
D. 12
C
搞不懂了真的