Python 如何每隔一次将金额增加两倍?
我不知道如何将金额增加三倍Python 如何每隔一次将金额增加两倍?,python,Python,我不知道如何将金额增加三倍 base = 1 payments = 5 print("Month 1: %s" % base) for i in range(2, payments): if i % 2 == 1: base *= 3 else: base *= 2 print("Month %s: %s" % (i+1, base)) 第一个月的付款1美元 第二个月的付款2美元。(金额增加了一倍) 第三个月的付款6美元。(每隔几个月
base = 1
payments = 5
print("Month 1: %s" % base)
for i in range(2, payments):
if i % 2 == 1:
base *= 3
else:
base *= 2
print("Month %s: %s" % (i+1, base))
第一个月的付款1美元
第二个月的付款2美元。(金额增加了一倍)
第三个月的付款6美元。(每隔几个月增加三倍)
第四个月的付款12美元。(双倍金额)
第五个月付款36美元。(每隔一个月增加三倍)
第六个月付款72美元。(双倍金额)
第7个月的付款216美元。(每隔一个月增加三倍)
等等
我使用for和if语句
base = 1
payments = int(input("For how many months did they say you will receive payments? "))
for i in range(0, payments):
if i % 2 > 0:
base *= 3
else:
base *= 2
month = "Month " + str(i + 1) + ":" + str(base)
print(month)
第一个月我得到2美元,我希望得到1美元,你可以使用操作符,在每一个单数条目上,将金额增加三倍
base = 1
payments = 5
print("Month 1: %s" % base)
for i in range(2, payments):
if i % 2 == 1:
base *= 3
else:
base *= 2
print("Month %s: %s" % (i+1, base))
当分期付款少于输入时,您可以使用要乘以(2和3)的数字列表。逻辑是在条件为真时在列表的两个数字之间交替:
base = 1
payments = input("For how many months did they say you will receive payments? ")
x = 1
multiplyList = [2, 3]
print(f'Month {x}: $ {base}')
while x <= int(payments):
i = 0
for number in multiplyList:
base = (base * multiplyList[i])
print(f'Month {x}: $ {base}')
i = i + 1
x = x + 1
# output:
# Month 1: $ 1
# Month 1: $ 2
# Month 2: $ 6
# Month 3: $ 12
# Month 4: $ 36
# Month 5: $ 72
# Month 6: $ 216
# Month 7: $ 432
# Month 8: $ 1296
# Month 9: $ 2592
# Month 10: $ 7776
base=1
付款=输入(“他们说您将收到多少个月的付款?”)
x=1
multiplyList=[2,3]
打印(f'Month{x}:${base}')
而x
编辑:OP对问题进行了编辑,以纳入新的尝试和建议
更改问题陈述,使其过时
正如在其他答案中提到的,您的方法有一些缺点,这使得它不是理想的解决方案
也就是说,这里是代码出错的地方:
从原始版本的压缩版本开始:
base = 1
payments = 10
for i in range(payments):
month = "Month " + str(i + 1) + ":" + str(base)
base *= 2
if i in range(2, payments, 3):
base *= 3
print(month)
你需要在这里结束:
base = 1
payments = 10
for i in range(payments):
month = "Month " + str(i + 1) + ":" + str(base)
if i in range(1, payments, 3):
base *= 3
else:
base *= 2
print(month)
所需的改变是:
- 使用
范围(2,…)
而不是范围(1,…)
。这是因为打印和计算的方式最终决定了上个月的新基数
- 将
*=2
移动到else:
语句中,这样就不会乘以6
这项工作:
base = 1
payments = int(input("For how many months did they say you will receive payments? "))
month = "Month " + str(1) + ":" + str(base)
print(month)
for i in range(1, payments):
if i % 2 > 0:
base *= 2
else:
base *= 3
month = "Month " + str(i + 1) + ":" + str(base)
print(month)
因为你直接进入了for
循环,这意味着你在第一个月就翻了一番。但是,如果您在循环之前打印第一个金额,从两个开始循环,并交换模运算语句,它就可以工作。此解决方案只有一个print()语句来打印月份和金额,但循环中有一个if==0
可以在循环中去掉if语句,但必须在循环之前再添加一行打印
如果不想打印中间结果,则可以从1开始,如果i==0,则获取的ird,并在离开for循环后打印结果
base = 1
payments = int(input("For how many months did they say you will receive payments? "))
print(base)
for i in range(0, payments):
if i == 0:
pass
elif i % 2 > 0:
base *= 2
else:
base *= 3
msg = "Month " + str(i + 1) + ":" + str(base)
print(msg)
我不明白,所以理解模运算是关键。您可以使用wiki链接查看它。我们知道我们需要每隔一个月在翻倍和翻倍之间切换,我们可以在计数时将偶数和奇数(不等于2的数)的“切换”联系起来。模运算符是一种工具,用于查找一个数字除以另一个数字后的余数。第1个数字应为$1@hewiepx1-忘记了一个细节,所以我再次更新了它“第一个月我得到2美元,我希望得到1美元”为什么?循环内的乘法是在打印之前还是之后发生的?因此,在第一次打印之前是否会发生乘法运算?追溯逻辑。