19.9k views
2 votes
Python: Can someone help put an exception handling in this program?

def main():
bill = float(input('Enter total owe: '))
paid = float(input('Enter amount given: '))
change = paid - bill
if change < 0:
print('You paid less than the money you owe')
return
cents = int(change * 100)
bill20, cents = cents // 2000, cents % 2000
bill10, cents = cents // 1000, cents % 1000
bill5, cents = cents // 500, cents % 500
bill1, cents = cents // 100, cents % 100
quarter, cents = cents // 25, cents % 25
dime, cents = cents // 10, cents % 10
nickel, cents = cents // 5, cents % 5
pennies = cents
print('\\Here is the change for $', change, 'amount')
print(f'{bill20} x $ 20 bill(s)')
print(f'{bill10} x $ 10 bill(s)')
print(f'{bill5} x $ 5 bill(s)')
print(f'{bill1} x $ 1 bill(s)')
print(f'{quarter} x quarter coin(s)')
print(f'{dime} x dime coin(s)')
print(f'{nickel} x nickel coin(s)')
print(f'{pennies} x penny coin(s)')
main()
What I want is that if the money given is less than the money owed, the program is going to tell me something like "You paid less than the money you owed" and the program will start again.

1 Answer

4 votes

Final answer:

To add exception handling to the given Python program, you can use the try-except block. Here's the modified code:

Here is the change for $', change, 'amount') print(f'{bill20} x $ 20 bill(s)') print(f'{bill10} x $ 10 bill(s)') print(f'{bill5} x $ 5 bill(s)') print(f'{bill1} x $ 1 bill(s)') print(f'{quarter} x quarter coin(s)') print(f'{dime} x dime coin(s)') print(f'{nickel} x nickel coin(s)') print(f'{pennies} x penny coin(s)') except ValueError as e: print(e) main() main()

This modified code will handle the situation where the money given is less than the money owed and display the appropriate message. It will then start the program again.

Step-by-step explanation:

To add exception handling to the given Python program, we can use the try-except block. Here's how:

  1. Enclose the code that may raise an exception within a try block.
  2. Specify the type of exception you want to catch using the except keyword, followed by the exception type.
  3. Write the code to handle the exception within the except block.
  4. If an exception occurs within the try block, the program will jump to the corresponding except block and execute the code inside it.
  5. If no exception occurs, the code within the except block will be skipped.

In this case, we want to handle the situation where the money given is less than the money owed. We can use a ValueError exception to catch this scenario. Here's the modified code:

Here is the change for $', change, 'amount') print(f'{bill20} x $ 20 bill(s)') print(f'{bill10} x $ 10 bill(s)') print(f'{bill5} x $ 5 bill(s)') print(f'{bill1} x $ 1 bill(s)') print(f'{quarter} x quarter coin(s)') print(f'{dime} x dime coin(s)') print(f'{nickel} x nickel coin(s)') print(f'{pennies} x penny coin(s)') except ValueError as e: print(e) main() main()

Now, if the money given is less than the money owed, a ValueError exception will be raised, and the program will display the message 'You paid less than the money you owe' and start again by calling the main() function recursively.

User Richard Sweeney
by
7.9k points