作者FrockYu (陈俊杰铁粉)
看板Python
标题[问题] 菜鸡问题求助
时间Sun Jun 28 06:54:50 2020
安安,小弟最近正在学习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://webptt.com/cn.aspx?n=bbs/Python/M.1593298492.A.31F.html
1F:推 yiche: 不清楚由使用者决定相加的过程要产生几次进位,是什麽意思 06/28 09:45
2F:→ yiche: ? 几次应该是你随机出的数字决定的 06/28 09:45
3F:→ yiche: 123+456 是0次, 987+654 是三次 06/28 09:46