0

Currently I'm trying to build a program that counts cash and change. When I am adding certain change though, it immediately calculates wrong and it is visible. It takes a few attempts to get it to occur but following these steps will cause the occurence.

1: Start the program by using python3 countcash.py

2: Press 1 to set 1 dollar bills to any # Enter number of ones: 15

3: Than begin adding random amounts of different change (pennies etc.)

Overtime it will begin to show the total as something like this Total = 15.99999999907

Now get ready... Here is all the code so far, all you have to do is possibly install a few of the imported libraries, than save with the .py extension and this is compatible with debian and windows systems!

from os import system, name
from time import sleep
import colorama as cl
import math as mt
from colorama import Fore, Style, Back
def rerun():
    rerun = True
    while rerun:
        program()
        rerun == 'y'
def clear():
    if not name == 'nt':
        _ = system('clear')
    else:
        _ = system('cls')
ones, fives, tens, twenties, fifties, hundreds, total, custom, change, deposit_add = 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
value_1, value_5, value_10, value_20, value_50, value_100, value_c, total_change = 0, 0, 0, 0, 0, 0, 0, 0
value_p, value_n, value_d, value_q = .01, .05, .1, .25
def program():
    clear()
    global ones, fives, tens, twenties, fifties, hundreds, value_1, value_5, value_10, value_20, value_50, value_100, value_c, custom, total, total_change, value_p, value_n, value_d, value_q, deposit, deposit_add
    total = value_1 + value_5 + value_10 + value_20 + value_50 + value_100 + value_c + total_change
    deposit = total - deposit_add
    print(f'''{Fore.RED}----------------------------------------------------{Style.RESET_ALL}
{Style.RESET_ALL}    1) Set 1\'s             {Fore.BLUE}1\'s =   {Fore.YELLOW}{ones} = {value_1}
{Style.RESET_ALL}    2) Set 5\'s             {Fore.BLUE}5\'s =   {Fore.YELLOW}{fives} = {value_5}
{Style.RESET_ALL}    3) Set 10\'s            {Fore.BLUE}10\'s =  {Fore.YELLOW}{tens} = {value_10}
{Style.RESET_ALL}    4) Set 20\'s            {Fore.BLUE}20\'s =  {Fore.YELLOW}{twenties} = {value_20}
{Style.RESET_ALL}    5) Set 50\'s            {Fore.BLUE}50\'s =  {Fore.YELLOW}{fifties} = {value_50}
{Style.RESET_ALL}    6) Set 100\'s           {Fore.BLUE}100\'s = {Fore.YELLOW}{hundreds} = {value_100}
{Style.RESET_ALL}    7) Set Penny\'s         {Fore.BLUE}Coin =  {Fore.YELLOW}{total_change}
{Style.RESET_ALL}    8) Set Nickel\'s        {Fore.BLUE}Total = {Fore.GREEN}{total}
{Style.RESET_ALL}    9) Set Dime\'s          {Fore.BLUE}Deposit = {Fore.RED}{deposit}
{Style.RESET_ALL}    10) Set Quarter\'s
{Style.RESET_ALL}    11) Custom
{Style.RESET_ALL}    12) Set Deposit
    
{Style.RESET_ALL}    0) Exit
{Fore.RED}----------------------------------------------------{Style.RESET_ALL}
''')
    choice = input('Enter Menu Option: ')
    if choice == '0':
        clear()
        exit()
    elif choice == '1':
        add_1 = int(input('\nEnter number of one\'s : '))
        value_1 = ones + add_1
        ones = ones + add_1
        rerun()
    elif choice == '2':
        add_5 = int(input('\nEnter number of five\'s : '))
        fives = fives + add_5
        value_5 = fives * 5
        rerun()
    elif choice == '3':
        add_10 = int(input('\nEnter number of ten\'s : '))
        tens = tens + add_10
        value_10 = tens * 10
        rerun()
    elif choice == '4':
        add_20 = int(input('\nEnter number of twenties : '))
        twenties = twenties + add_20
        value_20 = twenties * 20
        rerun()
    elif choice == '5':
        add_50 = int(input('\nEnter number of fifties : '))
        fifties = fifties + add_50
        value_50 = fifties * 50
        rerun()
    elif choice == '6':
        add_100 = int(input('\nEnter number of hundreds : '))
        hundreds = hundreds + add_100
        value_100 = hundreds * 100
        rerun()
    elif choice == '7':
        penny = float(input('\nEnter number of pennies : '))
        add_penny = penny * value_p
        total_change = total_change + add_penny
        rerun()
    elif choice == '8':
        nickel = float(input('\nEnter number of nickel\'s : '))
        add_nickel = nickel * value_n
        total_change = total_change + add_nickel
        rerun()
    elif choice == '9':
        dime = float(input('\nEnter number of dimes : '))
        add_dime = dime * value_d
        total_change = total_change + add_dime
        rerun()
    elif choice == '10':
        quarter = float(input('\nEnter number of quarter\'s : '))
        add_quarter= quarter * value_q
        total_change = total_change + add_quarter
        rerun()
    elif choice == '11':
        custom = float(input('\nEnter custom amount : '))
        value_c += custom
        rerun()
    elif choice == '12':
        deposit_add = float(input('\nEnter deposit amount : '))
        rerun()
    else:
        print('\n' + choice + ' is not an option...')
        sleep(.5)
        rerun()
rerun()'''
Cyber Guy
  • 7
  • 3
  • 3
    Floating point can't represent all fractions perfectly. Use integers and track pennies instead of dollars. See the duplicates for the "why" – Mark Tolonen Jul 29 '21 at 23:14
  • When you say wrong, do you mean 15.9999999997 is supposed to be 16.00? Then yes, it's just that floats are (fairly good) approximations. Adding up many approximations builds up error until you can see it. If you don't mean this, then it's a different problem and you need to clarify what you think the right answer should've been. – BatWannaBe Jul 29 '21 at 23:20

0 Answers0