我有一个代码,它有一个分数计数器,不断重复它的

2024-10-03 13:18:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我有个家庭作业,就是做个测验。它必须包含一个预定义函数的列表。我对python和代码非常陌生,只能理解基础知识。在我的测验中,我有一个计数器,可以记录问题的对错,并在测验结束时显示出来。当前,摘要会重复倒数计时,直到找到正确答案。我很困惑,明天就要考试了。如果有人有一个简单的记分器,我可以用它来代替我的记分器,也可以帮助修复我的记分器,我将不胜感激:)

我看了代码,但不能确定原因,因为我是新来的

k = 1 
while k==1:
#asks user a question
  print("Q10 - When was the first ever official Formula 1 race?\n1:1850 2:1950 or 3:Yesterday")
  q1 = input ("\n")
  intcheck(q1)
#correct answer
  if q1 == "2":  
    r.append(1)
    print("Congrats you got it correct\n")
#wrong answer
  else:  
    w.append(1)
    print("Tough luck, you got that one wrong!")

# score counter
while len(r) > 0:
  resultr += 1
  r.remove(1)
while len(w) > 0:
  resultw += 1
  w.remove(1)

#final scoreboard
  print ("===============================================")
  print ("----------------End Game Summary---------------")
  time.sleep(0.5)
  print ("You got",resultw,"wrong and ",resultr," correct")
  time.sleep(3)
  print ("              !Thanks for playing!             ")
  print ("===============================================")

Tags: 代码answeryoulenremoveresultrprintgot
2条回答

以下代码可以作为基线:

import time

question_list = ["Q1 blabla", "Q2 blala", "Q3 blabla"]
size_question_list = len(question_list)
answer_list = ["answer 1", "answer 2", "answer 3"]

correct_answers = []
for k in range(size_question_list):
    #asks user a question
    print(question_list[k])
    q = input ("your answer : ")

    if q == answer_list[k]:
        # Right answer
        correct_answers.append(k)
        print("Congrats you got it correct\n")
    else:
        # wrong answer
        print("Tough luck, you got that one wrong!")

#final scoreboard

print ("===============================================")
print ("        End Game Summary       -")
time.sleep(0.5)
print ("You got",size_question_list - len(correct_answers),"wrong and ",len(correct_answers)," correct")
time.sleep(3)
print ("              !Thanks for playing!             ")
print ("===============================================")

由于您是python新手,我将向您介绍一点面向对象编程

import time

class Question:
    def __init__(self, question, answers, solution):
        self.question = question
        self.answers = answers
        self.solution = solution
    def ask_question(self):
        print(self.question)
        for ix, rep in enumerate(self.answers):
            print(ix, ':', rep)
        user_rep = int(input())
        if user_rep == self.solution:
            print("Congrats you got it correct")
            return 1
        else:
            print("Tough luck, you got that one wrong!")
            return 0


question_10 = "Q10 - When was the first ever official Formula 1 race?"
anwers_10 = ['1850', '1950', 'Yesterday']
solution_10 = 1

questions = [(question_10, anwers_10, solution_10)]

right, wrong = 0, 0
for q in questions:
    quest = Question(q[0], q[1], q[2])
    user_rep = quest.ask_question()
    if user_rep == 1:
        right += 1
    else:
        wrong += 1


#final scoreboard


print ("===============================================")
print ("        End Game Summary       -")
time.sleep(0.5)
print ("You got",wrong,"wrong and ",right," correct")
time.sleep(3)
print ("              !Thanks for playing!             ")
print ("===============================================")

你只要把每个问题都写成这样:

question_10 = "Q10 - When was the first ever official Formula 1 race?"
anwers_10 = ['1850', '1950', 'Yesterday']
solution_10 = 1

然后将每个问题添加到循环之前的questions列表中

随便问什么问题

相关问题 更多 >