有没有大佬能帮我改一下一个python程序啊
python3吧
全部回复
仅看楼主
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
level 1
简单写了一个
def load_questions():
with open('Q.txt', 'r') as Q:
lines = Q.readlines()
step = 6
questions = [lines[i:i+step] for i in range(0,len(lines),step)]
return questions
def print_list(input_list):
for _ in input_list:
print(_)
student_id = input('student_id: ')
student_name = input('student_name: ')
score = 0
questions = load_questions()
for index, que in enumerate(questions):
q = que[:-1]
a = que[-1:][0].strip()
print_list(q)
user_a = input('answer: ')
if index + 1 == len(questions):
print('Finished!')
elif user_a == a:
print('CORRECT. Load next question..\n')
score += 1
else:
print('WRONG. The answer is', a)
with open('R.txt', 'w') as R:
R.write('Name: ' + student_name + '\n'
'UID: ' + student_id + '\n'
'Score: ' + str(score))
2021年05月11日 04点05分 2
多谢大哥
2021年05月13日 05点05分
1