-1

I want to display a message that says "Invalid Discount, Discount should have value 0 to 99" if the user input a user input a number lesser than 0 or greater than 99. I tried to do this on my own, but my program seem to get confused every time i input a number at toyDiscount.

count = "yes"
toyAmount = 1
finalPrice = 0
x = 1
y = 1

while x <= toyAmount:
  if count == "yes":

    while True:
      toyPrice = int(input("Price of Toy " + str(x) + " = Rp."))
      if toyPrice <= 0:
        print("Invalid Price! The price should be greater than 0")
        print("")
      else:

        while True:
          toyDiscount = int(input("Discount #" + str(y) + "(in %): "))
          if toyDiscount == 0:
            print("Price = " + str(toyPrice))
            break
          else:
            toyPrice = toyPrice * (1 - toyDiscount/100)
            y = y + 1
                          
        finalPrice += toyPrice
        print("")
        count = input("More Toys to buy (yes/no)? = ")
        print("")
        x += 1
        toyAmount += 1
        y=1
        break

  else:
    coup = finalPrice // 25000
    print("Final Price = " + str(finalPrice))
    print("You get %d gift voucher" %(coup))
    break
Siddhartha
  • 311
  • 3
  • 8
Ginko
  • 13
  • 4
  • A little off-topic, but I think you would find the answers to [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) useful to also know. – martineau Sep 07 '21 at 17:55

2 Answers2

1

You can add a conditional statement inside your second loop.

count = "yes"
toyAmount = 1
finalPrice = 0
x = 1
y = 1

while x <= toyAmount:
  if count == "yes":

    while True:
      toyPrice = int(input("Price of Toy " + str(x) + " = Rp."))
      if toyPrice <= 0:
        print("Invalid Price! The price should be greater than 0")
        print("")
      else:

        while True:
          toyDiscount = int(input("Discount #" + str(y) + "(in %): "))
          if toyDiscount < 0 or toyDiscount > 99:
            print("Invalid Discount, Discount should have value 0 to 99")
            continue
          if toyDiscount == 0:
            print("Price = " + str(toyPrice))
            break
          else:
            toyPrice = toyPrice * (1 - toyDiscount/100)
            y = y + 1
                          
        finalPrice += toyPrice
        print("")
        count = input("More Toys to buy (yes/no)? = ")
        print("")
        x += 1
        toyAmount += 1
        y=1
        break

  else:
    coup = finalPrice // 25000
    print("Final Price = " + str(finalPrice))
    print("You get %d gift voucher" %(coup))
    break
Siddhartha
  • 311
  • 3
  • 8
0

Try this:

count = "yes"
toyAmount = 1
finalPrice = 0
x = 1
y = 1

while x <= toyAmount:
    if count == "yes":

        while True:
            toyPrice = int(input("Price of Toy " + str(x) + " = Rp."))
            if toyPrice <= 0:
                print("Invalid Price! The price should be greater than 0")
                print("")
            else:

                while True:
                    toyDiscount = int(input("Discount #" + str(y) + "(in %): "))
                    if toyDiscount == 0:
                        print("Price = " + str(toyPrice))
                        break
                    elif toyDiscount < 0 or toyDiscount > 99:
                        print("Please enter a value between 0 and 99")
                    else:
                        toyPrice = toyPrice * (1 - toyDiscount / 100)
                        y = y + 1

                finalPrice += toyPrice
                print("")
                count = input("More Toys to buy (yes/no)? = ")
                print("")
                x += 1
                toyAmount += 1
                y = 1
                break

    else:
        coup = finalPrice // 25000
        print("Final Price = " + str(finalPrice))
        print("You get %d gift voucher" % (coup))
        break

You can add another condition to the toyDiscount input.

User One
  • 287
  • 2
  • 9