簡體   English   中英

我有一個帶有計分器的代碼,該計分器不斷重復

[英]I have a code that has a score counter that keeps repeating itself

我的一項家庭作業是創建測驗。 它必須包含一個預定義函數的列表。 我對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 ("===============================================")

以下代碼可以作為基准:

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列表中。

隨意問任何問題!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM