python - 如何重置我的代码的折扣计数?
问题描述
如何重置“折扣”计数?正如您在“玩具 2 的价格”中看到的,折扣显示为“折扣 3、4 和 5”我如何重置此计数并在“玩具 2 的价格”处显示“折扣 1、2 和 3”谢谢你的帮助
count = "yes"
discount = 0
x = 1
y = 1
toyAmount = 1
while x <= toyAmount:
if count == "yes":
toyPrice = int(input("Price of Toy " + str(x) + " = Rp."))
while True:
toyDiscount = int(input("Discount #" + str(y) + ": "))
if toyDiscount != 0:
y = y + 1
else:
break
count = input("More Toys? (yes/no) = ")
x += 1
discount += 1
toyAmount += 1
else:
break
**Output:**
Price of Toy 1 = Rp.20000
Discount #1: 40
Discount #2: 30
Discount #3: 0
More Toys? (yes/no) = yes
Price of Toy 2 = Rp.30000
Discount #3: 20
Discount #4: 50
Discount #5: 0
More Toys? (yes/no) = no
解决方案
y = 1
在代码末尾添加-
count = "yes"
discount = 0
x = 1
y = 1
toyAmount = 1
while x <= toyAmount:
if count == "yes":
toyPrice = int(input("Price of Toy " + str(x) + " = Rp."))
while True:
toyDiscount = int(input("Discount #" + str(y) + ": "))
if toyDiscount != 0:
y = y + 1
else:
break
count = input("More Toys? (yes/no) = ")
x += 1
discount += 1
toyAmount += 1
y = 1 # Add it here, so that discount starts from one
else:
break
结果:
Price of Toy 1 = Rp.50
Discount #1: 2
Discount #2: 0
More Toys? (yes/no) = yes
Price of Toy 2 = Rp.20
Discount #1: 1
Discount #2: 0
More Toys? (yes/no) = no
推荐阅读
- javascript - 使用 d3.js 创建径向(圆形)分组条形图
- link-to - Rails 5 无法完成“belongs_to”
- java - JVM堆栈中的局部变量表
- python - 通过预测从 sklearn 预测 1 个值
- vector - (移动)嵌套向量上的 push_back
- sql - 根据 CASE 表达式加入 2 个不同的条件
- spring-data - 是时候在集装箱上居住财产了
- java - JPA 结果集映射到新对象
- python - 终端中 child_process 的返回值
- lua - 我刚开始在 lua-roblox 中编码,我面临尝试用“值”索引 nil,但不知道为什么