Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python问答题_Python_Python 3.x - Fatal编程技术网

Python问答题

Python问答题,python,python-3.x,Python,Python 3.x,所以,我必须设计一个基本的测验,目前它相当简单。 基本上,我有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 tempe

所以,我必须设计一个基本的测验,目前它相当简单。 基本上,我有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

您需要有一个while循环,用于检查以下任一条件:

import random
import sys

# qa = [...]

random.shuffle(qa)

correct = 0
incorrect = 0

while correct != 3 or incorrect != 7:
    for q,a in qa:
       print('Your current score is: {}'.format(correct))
       print('Your percentage is: {}'.format((correct/10)*100))
       user_answer = input(q)
       if user_answer.lower() == a.lower():
           correct +=1
           print('Correct!')
       else:
           incorrect += 1
           print('Incorrect! The correct answer is {}'.format(a))

if correct == 3:
   print('You won!')
if incorrect == 7:
   print('You lose!')

# End of code

我试过@Pushkin,但是它不起作用,因为中断,我得到了一个语法错误。它没有打印出导致错误的特定代码行吗?您使用的是哪个版本的Python?它对我有用。如果是Python 2,您可能希望将
input(q)
更改为
raw\u input(q)
。3.4.1版使用您的方法时,它甚至不会运行?@JUnit\u 2还确保您具有一致的缩进。我之所以给您一个+1,仅仅是因为我喜欢琐事。这在我的实际代码中是什么,当我试图将其应用到我的代码中时,它不起作用@布尔汉·哈立德