1

how do i do an insufficient cash output when the difference from the cash and the product is in the negative

cash = float(input('How much money did you recieve? '))
acer1 = (299.99)
acer2 = (399.99)
acer3 = (499.99)
insufficient_balance = 'Insufficient Cash'
negative_numbers = -1
while negative_numbers < -999_999:
    print(negative_numbers)
    negative_numbers += -1
model = input('What model do you want? ')
if model == 'acer1':
    print('Change: ')
    print(cash - acer1)
    print('Thank you for shopping with us')
elif model == 'acer2':
    print('Change: ')
    print(cash - acer2)
    print('Thank you for shopping with us')
elif model == 'acer3':
    print('Change: ')
    print(cash - acer3)
    print('Thank you for shopping with us')
tripleee
  • 175,061
  • 34
  • 275
  • 318
  • 1
    I think if I ever teach in a coding school I'll make everyone write on a dry erase board 100 times "[Thou shalt not use floating point numbers for currency](https://stackoverflow.com/questions/588004/is-floating-point-math-broken)". And maybe add [especially in python...](https://docs.python.org/3/library/decimal.html) Also why did you tag this with 2.7 *and* 3.x? – Jared Smith Dec 15 '20 at 16:07

1 Answers1

0

After calculating change use if/else to either print error message if change is negative or success message.

if model == 'acer1':
    change = cash - acer1
    if change < 0:
        print('Insufficient Cash')
    else:
        print(f'Change: {change}')
        print('Thank you for shopping with us')

You can use a dictionary to simplify things a bit:

inventory = {
     'acer1' : 299.99,
     'acer2' : 399.99,
     'acer3' : 499.99
}
cash = float(input('How much money did you receive? '))
model = input('What model do you want? ').lower()
if model in inventory:
    change = cash - inventory[model]
    if change < 0:
        print('Insufficient Cash')
    else:
        print(f'Change: {change}')
        print('Thank you for shopping with us')

Lastly, it is usually not a good idea to use floating point for money due to rounding errors.

001
  • 13,291
  • 5
  • 35
  • 66