Skip to main content
 首页 » 编程设计

Python 测验问题

2024年10月24日13hnrainll

所以,我必须设计一个基本的测验,目前它相当简单。 本质上,我有 10 个问题,我已经整合了一个评分系统,并且我已经设法创建了一个百分比系统。

这是我的代码:

# Quiz Game 
 
import random 
import sys 
 
 
 
score = 0 # -> initial score 
print("Your score is currently " + str(score)) 
 
qa = [('In which Australian state was the highest temperature of 53 deg C recorded? ', 'Queensland'), 
      ('What animal is featured on the 2c coin? ', 'Frilled neck lizard'), 
      ('What Australian company is the largest surfwear manufacturer? ' , 'Quicksilver'), 
      ('How many ships were in the First Fleet? ', '11'), 
      ('In what year was decimal currency introduced in Australia? ', '1966'), 
      ('What was Sir Donald Bradman’s batting average? ', '99.94'), 
      ('How much of Australia is classified as desert A) 8% B) 16% C) 25% D) 35% ', 'D'), 
      ('On which Australian decimal banknote did a portrait of Henry Lawson appear? A) $5 B) $10 C) $20 D) $50 E) $100 ','B'), 
      ('True or False, Bathurst is held at Mt. Panome? ', 'False'), 
      ("Who was Australia's former F1 driver to Daniel Ricciardo? ", 'Mark Webber')] 
 
random.shuffle(qa) 
 
for q,a in qa: 
  user_answer = input(q) 
  if user_answer.lower() == a.lower(): 
# -> determining if the answer is correct or not 
    print("Correct!") 
    score = score+1 
    print('Your score is currently ' + str(score)) # -> scoring system 
    print ("Your Percentage is: ") 
    print((score/10)*100) # -> this is the percentage calculation 
  if score==2: 
    print('Congratualtions, you have beaten the quiz') 
 
 
  else: 
    print("Incorrect!") 
    print("The answer is " + a) 

我目前的问题是我不确定如何执行以下操作: 1) 如果用户得到 7 个正确答案,提前退出并显示获胜者消息。 2) 如果用户得到 3 个错误答案,则提前退出并显示失败消息。

我该怎么做?我是 python 的新手,有一段时间没有使用它,所以我有点生疏。我需要保留这个平台,但如果你们能想到任何补充,我将不胜感激。

先谢谢你。

请您参考如下方法:

这里有一些伪代码可以帮助您入门。您的想法基本上是正确的。

score = 0 
incorrect = 0 
for q,a in qa: 
    user_answer = input(q) 
    if user_answer.lower() == a.lower(): 
        score += 1 
    else: 
        incorrect += 1 
 
    if score == 7: 
        print('some message') 
        break 
    if incorrect == 3: 
        print('some message') 
        break