0
flag = 'y'
while flag == 'y':
    try:
        item_price = float(input('Enter item price: '))
        item_quantity = float(input('Enter the item quantity: '))
        if item_price > 0 and item_quantity > 0:
            sub_total = item_quantity * item_price
            total = sub_total + sub_total * 0.0825
            print(f'Subtotal is ${sub_total}')
            print(f'Total is ${round(total,2)}')
        else:
            print('Error: Enter a positive number for item price and quantity.')

    except ValueError:
        print('Error: Please enter a number!')

    flag = input('Do you want to continue (y/n)?\n')

In this case I can enter a negative item price and only after entering quantity, the Error: Enter a positive number for item price and quantity. is displayed. How do I display this error if the item price is negative ?

Chandan
  • 571
  • 4
  • 21
Cruise5
  • 217
  • 1
  • 12
  • 3
    Does this answer your question? [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) – AMC Mar 16 '20 at 03:20

3 Answers3

1

So the problem is where you are checking if the number is negative

try:
    item_price = float(input('Enter item price: '))
    if item_price > 0: # Notice where this if is placed.
        item_quantity = float(input('Enter the item quantity: '))
        if item_quantity > 0:
            sub_total = item_quantity * item_price
            total = sub_total + sub_total * 0.0825
            print(f'Subtotal is ${sub_total}')
            print(f'Total is ${round(total,2)}')
        else:
            print('Error: Enter a positive number for item price and quantity.')
    else:
        print('Error: Enter a positive number for item price and quantity.')

If you split the if statement up, you can check for a positive price before it even asks for the quantity

Rashid 'Lee' Ibrahim
  • 1,357
  • 1
  • 9
  • 21
  • If I enter a correct price for the item and incorrect number for qty, it's restarting the while loop to enter the price again. How can I ask it just correct the qty after entering the correct price ? – Cruise5 Mar 16 '20 at 01:30
  • That's a separate question, but you will need to restructure your code. You could simple add another loop inside the try function to check for quantities, but that will make the code even more messy than it is now. At this point you need some redesign to make the code a little more efficient. You might want to go ask on the software exchange site. – Rashid 'Lee' Ibrahim Mar 16 '20 at 01:42
1

If you want to use exceptions, you can raise different ValueErrors for each case, and then use a string method to differentiate them and decide what kind of error to print to the caller:

try:
    item = float(input('price:'))
    if item <= 0:
        raise ValueError('Error: Enter a positive number')

#here handle the different possible exception strings individually:
except ValueError as e:
    if (str(e).startswith('Error')):
        print(str(e))
    else:   #the built-in would be 'cannot convert str to float'
        print('Error: please enter a number')
neutrino_logic
  • 1,289
  • 1
  • 6
  • 11
0

This would work.

class CustomValueError(ValueError):
 def __init__(self, arg):
  self.strerror = arg
  self.args = {arg}

flag = 'y'
while flag == 'y':
    try:
        item_price = float(input('Enter item price: '))
        if item_price > 0:
            item_quantity = float(input('Enter the item quantity: '))
                if item_quantity > 0:
                    sub_total = item_quantity * item_price
                    total = sub_total + sub_total * 0.0825
                    print(f'Subtotal is ${sub_total}')
                    print(f'Total is ${round(total,2)}')
                else:
                    try:
                        raise CustomValueError("Error: Enter a positive number for item price and quantity.")
        else:
            try:
                raise CustomValueError("Error: Enter a positive number for item price and quantity.")

    except ValueError:
        print('Error: Please enter a number!')

    flag = input('Do you want to continue (y/n)?\n')
jinwon2
  • 84
  • 11