iT邦幫忙

0

問答程式跑不出題目

  • 分享至 

  • xImage
from untitled5 import Question        

choose1=[
    "有沒有想我啊?\n (a)當然摟 想死你了\n (b)怎麼可能 我已經忘記你勒\n",
]
questions = [
    Question(choose1[0],"a"),
]
def test(questions):
    for question in questions:        
        answer = input(question.description)
        if answer == question.answer:
            print("太棒了","看你疑惑的眼神,我還以為你忘了我(","給予","一個大大的擁抱")
               
        if answer != question.answer:
            print("琳達:真沒想到你竟然忘了我,我真的好難過")
            print("琳達跑走了")
class Question:
    def __init__(self,description,answer):
        self.description=description
        self.answer=answer

根據影片做了個問答程式 有小改過 但run的時候什麼都沒發生 很好奇怎麼沒跑出題目
請大神解惑

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

1
海綿寶寶
iT邦大神 1 級 ‧ 2022-06-10 15:12:36
最佳解答

因為少了一列
test(questions)

加入後大概像這樣

...
questions = [
    Question(choose1[0],"a"),
]
test(questions)
...
貓虎皮 iT邦新手 3 級 ‧ 2022-06-11 12:00:03 檢舉

test(questions)必須至於def test(questions):

像這樣:

class Question:
    def __init__(self,description,answer):
        self.description=description
        self.answer=answer

choose1=[
    "有沒有想我啊?\n (a)當然摟 想死你了\n (b)怎麼可能 我已經忘記你勒\n",
]
questions = [
    Question(choose1[0],"a"),
]
# test(questions) # 如果在這裡呼叫函式的話,會應為函式尚未被宣告而出現NameError錯誤。
def test(questions):
    for question in questions:        
        answer = input(question.description)
        if answer == question.answer:
            print("太棒了","看你疑惑的眼神,我還以為你忘了我(","給予","一個大大的擁抱")
               
        if answer != question.answer:
            print("琳達:真沒想到你竟然忘了我,我真的好難過")
            print("琳達跑走了")
test(questions) # 加在這裡!

恭喜你自己解決一個問題了

對 我後來有發現加在最後面
感謝幫助 之前因為任務沒完成沒法回復

alexachris
iT邦見習生 ‧ 2022-06-10 19:30:13
【**此則訊息已被站方移除**】

我要發表回答

立即登入回答