亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Python 中的測驗 - 原始程序已運行,但已重構且現在無法運行

Python 中的測驗 - 原始程序已運行,但已重構且現在無法運行

九州編程 2021-11-02 20:31:42
Python 中的簡單測驗,它以“answer,question”格式讀取 csv 文件。在我決定嘗試重構我的原始代碼之前,該程序正在運行。我參考了不同的來源來確定 csv、random、classes 和 loop 是否正確編碼,但代碼不能使用這個更新的重構版本運行。原始代碼player_name = input("What is your name? ")print(f"Welcome, {player_name}, to Quiz!")play_quiz = str(input("Are you ready to play?\n"))if play_quiz != "y":    exit()class Question:    def __init__(self, prompt, answer):        self.prompt = prompt        self.answer = answer# sample quiz questions go here where ["Question", "Answer choices from a-c"]question_prompts = []questions = [        Question(question_prompts[0], "b"),        Question(question_prompts[1], "a"),    ]def run_test(questions):    score = 0    for question in questions:        answer = input(question.prompt)        if answer == question.answer:            score += 1        print("You answered " + str(score) + "/" + str(len(questions)) + " correct.")    return input("Want to play again? (y/n): ") == "y".lower()play_again = Truewhile play_again:    play_again = run_test(questions)重構代碼import csvimport random player_name = input("What is your name? ")print(f"Welcome, {player_name}, to the Quiz!")play_quiz = str(input("Are you ready to play? "))if play_quiz != "y":    exit()class Question:    def __init__(self, prompt, answer):        self.prompt = prompt        self.answer = answerdef quiz():    score = 0    questions_right = 0    quiz_file = open(characters_file, "r")    quiz_data = quiz_file.readlines()    random.shuffle(quiz_data)    question_number = 1     for question in range(65):         x = quiz_data[question].strip()         data = x.split(",")         Question = data.prompt[1]         correct_answer = data.answer[1]def run_test(quiz_data):    answer = input("What is your answer? ")    if answer == correct_answer:         score += 1         question_right = question_number + 1    else:         print("Incorrect.")         print(f"Correct answer should be: {CorrectAnswer}")我不明白我做錯了什么,因為它沒有使用下面的重構代碼運行。本質上,我做錯了什么?我是否將代碼放在錯誤的位置以使其無法運行?
查看完整描述

1 回答

?
楊魅力

TA貢獻1811條經驗 獲得超6個贊

我認為您錯誤地使用了Question您制作的課程。你試圖做Question = data.prompt[1]這并沒有真正意義。Question 是一個類,因此您將使用它來創建對象的實例。此外,您的班級期望值prompt并answer傳遞給它。按照您現在的設置方式,您可以按照new_question = Question("What color is the sky?", "blue"). 但是,我認為在此代碼中創建類沒有多大用處,因為您沒有附加任何方法......


下面是一個測驗類思想的例子,可以幫助你掌握 OOP 編程的概念:


import random


questions = [

    "What color is the sky?",

    "What year is it?"

]


answers = [

    "blue",

    "2019"

]


class Question:

    def __init__(self):

        index = random.randint(0, len(questions) - 1)

        self.answer = answers[index]

        self.question = questions[index]


    def check_valid(self, guess):

        if guess == self.answer:

            return True

        else:

            return False


if __name__ == "__main__":

    name = str(input("What's your name?\n"))

    print('Welcome then {} welcome to the quiz!'.format(name))

    while True:

        new_question = Question()

        check = False

        while check is not True:

            print(new_question.question)

            user_guess = str(input('What do you think the answer is?\n'))

            check = new_question.check_valid(user_guess)  

您可以看到,在該__init__(self):部分中,代碼并未真正進行任何主要計算,而只是設置了以后可以調用的內容,例如new_question.question. 但是,您可以連接方法,像類check_valid(由壓痕連接到類def),再后來就用在這些方法的實例創建類的。我的代碼中沒有很多函數(例如退出循環的能力),但希望這能幫助您更深入地理解 OOP!


查看完整回答
反對 回復 2021-11-02
  • 1 回答
  • 0 關注
  • 233 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號