首页 > 解决方案 > 如何重置我的代码的折扣计数?

问题描述

如何重置“折扣”计数?正如您在“玩具 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

标签: python

解决方案


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

推荐阅读