如何计算列表中的问题并进入下一阶段?

2024-10-03 04:32:56 发布

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

我是Python新手,正在制作一个测验应用程序。我在列表中总共有15个问题,但我只想在程序中问10个问题,我觉得这与列表中的计数有关,但我不知道如何做,一旦10个问题回答正确,用户将进入下一阶段的测验。这是我的密码:

# QuestionE.py (Name of my Python File)
class QuestionE:
    def __init__(self,prompt,answer):
        self.prompt = prompt
        self.answer = answer

# Main.py
from QuestionE import QuestionE
#15 questions
QE = ["I ______ tennis every Sunday morning\na. playing\nb. play\nc. am playing\nd. am play\nAnswer: ",
      "___________ is she? She's my friend from London.\na. Who\nb. Why\nc. Which\nd. What\nAnswer: ",
      "The baby _________ every night.\na. cries\nb. is crying\nc. cried\nd. cry\nAnswer: ",
      "_________ many times every winter in Frankfurt.\na. It snows\nb. It snowed\nc. It is snowing\nd. It is snow\nAnswer: ",
      "There ________ many flowers in the vase.\na. are\nb. have\nc. has\nd. is\nAnswer: ",
      "You have six flowers. Give _______ one, please.\na. you\nb. she\nc. me\nd. he\nAnswer: ",
      "Nid and Pon ________ go to the movie next Sunday.\na. are\nb. will\nc. is\nd. have\nAnswer: ",
      "Chai ________ English every day.\na. speak\nb. is speaking\nc. speaks\nd. spoke\nAnswer: ",
      "We have lunch ________ half past five.\na. of\nb. on\nc. in\nd. at\nAnswer: ",
      "A week ________ seven days.\na. has\nb. have\nc. are\nd. is\nAnswer: ",
      "We ________ throw litter on the ground.\na. isn't\nb. don't\nc. aren't\nd. haven't\nAnswer: ",
      "Weather report: It's seven o'clock in Frankfurt and ________\na. there is snow\nb. it's snowing\nc. it snows\nd. it snowed\nAnswer: ",
      "Jane ________ her blue jeans today, but usually she wears a skirt or a dress.\na. wears\nb. wearing\nc. wear\nd. is wearing\nAnswer: ",
      "I think I ________ a new calculator. This one does not work properly any more.\na. needs\nb. needed\nc. need\nd. am needing\nAnswer: ",
      "I've just finished reading a story called Dangerous Game. It's about a man who ________ his wife because he doesn't want to lose her.\na. kills\nb. killed\nc. kill\nd. is killing\nAnswer: "]


questions = [
    QuestionE(QE[0], "b"),
    QuestionE(QE[1], "a"),
    QuestionE(QE[2], "a"),
    QuestionE(QE[3], "a"),
    QuestionE(QE[4], "a"),
    QuestionE(QE[5], "c"),
    QuestionE(QE[6], "b"),
    QuestionE(QE[7], "c"),
    QuestionE(QE[8], "d"),
    QuestionE(QE[9], "a"),
    QuestionE(QE[10], "b"),
    QuestionE(QE[11], "b"),
    QuestionE(QE[12], "d"),
    QuestionE(QE[13], "c"),
    QuestionE(QE[14], "a"),
]
def run_test(questions):
    score = 0
    for question in questions:
        answer = input(question.prompt)
        if answer == question.answer:
            score += 1
            #print("You got " + str(score) + "/" + str(len(questions)) + " correct")
            print("Correct")
        else:
            print("Wrong")
            print ("You got " + str(score) + "/" + str(10) + " correct")
            print("Game Over! You did not passed the easy one")
            break
run_test(questions)

Tags: answerinishaveitpromptquestionsprint
3条回答

您可以在顶部执行import random

def myfunc():
    return 0.1
random.shuffle(questions,myfunc) #== Shuffle the questions
def run_test(questions):
    score = 0
    for i in range(10):
        answer = input(questions[i].prompt) #=== Get that question from the index
        if answer == questions[i].answer:
            score += 1
            #print("You got " + str(score) + "/" + str(len(questions)) + " correct")
            print("Correct")
        else:
            print("Wrong")
            
            print("Game Over! You did not passed the easy one")
            break
    print ("You got " + str(score) + "/" + str(10) + " correct")
run_test(questions)

下面是执行所需任务的更新代码

def run_test(questions):
    score = 0
    for i in range(10):
        answer = input(questions[i].prompt)
        if answer == questions[i].answer:
            score += 1
            print("Correct")
        else:
            print("Wrong")
            print("Game Over! You did not passed the easy one")
            break
    print ("You got " + str(score) + "/" + str(10) + " correct")
run_test(questions)

你可以试试看

import random

def run_test(questions):
    score = 0
    random.shuffle(questions)
    for question in questions[:10]:
         answer = input(question.prompt)
        ... 

相关问题 更多 >