[問題] 菜雞問題求助

看板Python作者 (陳俊傑鐵粉)時間3年前 (2020/06/28 06:54), 編輯推噓2(203)
留言5則, 3人參與, 3年前最新討論串1/1
安安,小弟最近正在學習def 的語法,遇到一題想請教各位: 題目大致敘述上是希望我們 (1)先設計出一個程式,隨機產生一個加法題,其加數與被加數將由使用者決定範圍,並由使用者決定加數、被加數在相加的過程總共要產生幾次進位。例如: 37 + 26即有1次進位(7+6)、238 + 179則有2次進位(3+7、8+9) (2)接著便仿造上一題,只是這次要產生10個隨機加法題,而範圍是從1000~9999,總進位數則是2次 我第一題的程式碼如下,執行起來是沒問題,但是第二題卻卡關很久,因為不知道怎麼用def並連續產生10題,想請各位大大幫忙,感謝! 第一題程式碼: import random low = int(input("Please enter the lower bound: ")) high = int(input("Please enter the higher bound: ")) difficulty = int(input("Please enter the desired difficulty: ")) if low < 0: low = -low if high < 0: high = -high if difficulty < 0: difficulty = -difficulty if low > high: low, high = high, low while True: op1 = random.randint(low,high) op2 = random.randint(low,high) op1_1 = op1 op2_2 = op2 count = 0 while op1 > 0 and op2 > 0: if (op1 % 10 + op2 % 10) >= 10: count += 1 op1, op2 = op1 // 10, op2 // 10 problem = 'Problem 1: %d + %d = ' %(op1_1, op2_2) if count == difficulty: print(problem,end='') break else: continue ----- Sent from JPTT on my Asus ASUS_X00ID. -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 49.216.65.222 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1593298492.A.31F.html

06/28 09:45, 3年前 , 1F
不清楚由使用者決定相加的過程要產生幾次進位,是什麼意思
06/28 09:45, 1F

06/28 09:45, 3年前 , 2F
? 幾次應該是妳隨機出的數字決定的
06/28 09:45, 2F

06/28 09:46, 3年前 , 3F
123+456 是0次, 987+654 是三次
06/28 09:46, 3F

06/28 10:53, 3年前 , 4F

06/29 16:28, 3年前 , 5F
工研院人工智慧課程推薦https://reurl.cc/4RDRaK
06/29 16:28, 5F
文章代碼(AID): #1UzyuyCV (Python)