看板 Python 關於我們 聯絡資訊
安安,各位程式高手,最近在自學python ,然後今天做到一題百思不得其解,想請問各位 題目大概的敘述是,要設計隨機的加減法問題,讓使用者輸入想答題的題數,然後當使用者答對,印出"good job",若是使用者答錯,一題有3次機會嘗試,若第三次仍答錯,則會印出"答案應該是___" 我大致上把程式架構都寫完了,答對輸出OK,答錯會輸出正確答案也OK,可是就是一直搞不定一題可以嘗試3次這部分,想請問各位該怎麼解? (然後因為目前只自修到布林運算,所以希望可以用布林運算 while for if等方法解釋,謝謝!) 以下是程式碼: n = int(input('Number of problems: ')) pass_ = 0 #答對題數 fail_ = 0 #答錯題數 count = 0 #單題答錯次數 import random for x in range(n): op1 = random.randint(10,100) op2 = random.randint(10,100) operator = random.choice(['+','-']) if operator == '+': solution = op1 + op2 else: if op1 < op2: op1,op2 = op2,op1 solution = op1 - op2 print(op1,operator,op2,'= ? ',end='') answer = int(input()) if answer == solution: print('Good job!') pass_ += 1 continue else: while count < 3: count += 1 fail_ += 1 if count == 3: fail_ += 1 print('Sorry, should be ',solution) print('Final score: ', 100 * pass_ / (pass_ + fail_)) ----- Sent from JPTT on my Asus ASUS_X00ID. -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 49.216.42.251 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1589476421.A.296.html
olycats: while那邊寫錯了喔 可能還有其他地方要改但這裡錯最明顯 05/15 06:04
IAMPF: while裡面要再有一個答題的過程 然後答對count要=0 05/15 08:37
moodoa3583: while換成if 05/15 09:51
ghoster7: 判斷count==3,那邊改成2 05/15 18:26
ghoster7: 你是從0開始做的,假如==3會做4次 05/15 18:26
FrockYu: 謝謝各位 05/16 04:14
cuteSquirrel: 一個輔助除錯的小工具 05/16 11:20
cuteSquirrel: http://www.pythontutor.com/visualize.html 05/16 11:20