Jump to content

Python Help. Please Help Me D:

Tohrchur

Hey guys, so I am supposed to write a program to take an input of "How much an item costs" and "how much you are paying with." So like the item is $550.32 and im paying with say, $1000. 

I did those numbers because I wanted to use every US bill.  

I will paste my code and then tell you what I did because its kind of weird. But I cant get the code to work and the reason is because if a bill is NOT used it creates an error and stop the if statements.  And I can't get around that.  So here is my code:

 

4zqcr.png

 

Then here is the actual code for copy/paste.

 

def makeChange(x,y):

    change=y-x

    a=change*100

    b=a+1

    change=int( B)

    print change

    if change>=10000:

        hundred=change/10000

        change=change-(10000*hundred)

        print hundred,"$100 bills"

    if change>=5000:

        fifty=change/5000

        change=change-(5000*fifty)

        print fifty,"$50 bills"

    if change>=2000:

        twenty=change/2000

        change=change-(2000*twenty)

    if change>=1000:

        ten=change/1000

        change=change-(1000*ten)

    if change>=500:

        five=change/500

        change=change-(500*five)

    if change>=100:

        one=change/100

        change=change-(100*one)

    if change>=25:

        quarter=change/25

        change=change-(25*quarter)

    if change>=10:

        dime=change/10

        change=change-(10*dime)

    if change>=05:

        nickel=change/05

        change=change-(05*nickel)

    if change>=01:

        penny=change/01

        change=change-(01*penny)

    #print hundred,fifty,twenty,ten,five,one,quarter,dime,nickel,penny

    #if change==0:

        #if quarter>1:

        #    print quarter, "quarters"

        #if quarter==1:

        #    print quarter, "quarter"

        #if dime>1:

        #    print dime, "dimes"

        #if dime==1:

        #    print dime, "dime"

        #if nickel>1:

        #    print nickel, "nickels"

        #if nickel==1:

        #    print nickel, "nickel"

        #if penny>1:

        #    print penny, "pennies"

        #if penny==1:

        #    print penny, "penny"

makeChange(550.32,1000.00)

 

 

Please help me :(

Tohrchur

Link to comment
Share on other sites

Link to post
Share on other sites

Small advice, but it will be helpful. Your code for getting the variable "change" is wrong.

 

For example:

 

int((1000-550.32)*100 + 1) = 44968, which is correct

 

but

 

int((1000-550.30)*100 + 1) = 44971, which is incorrect

 

You'll want to use python's round function:

 

change = round(y-x, 2) * 100 (round x-y to 2 decimal places)

I do not feel obliged to believe that the same God who has endowed us with sense, reason and intellect has intended us to forgo their use, and by some other means to give us knowledge which we can attain by them. - Galileo Galilei
Build Logs: Tophat (in progress), DNAF | Useful Links: How To: Choosing Your Storage Devices and Configuration, Case Study: RAID Tolerance to Failure, Reducing Single Points of Failure in Redundant Storage , Why Choose an SSD?, ZFS From A to Z (Eric1024), Advanced RAID: Survival Rates, Flashing LSI RAID Cards (alpenwasser), SAN and Storage Networking

Link to comment
Share on other sites

Link to post
Share on other sites

Small advice, but it will be helpful. Your code for getting the variable "change" is wrong.

 

For example:

 

int((1000-550.32)*100 + 1) = 44968, which is correct

 

but

 

int((1000-550.30)*100 + 1) = 44971, which is incorrect

 

You'll want to use python's round function:

 

change = round(y-x, 2) * 100 (round x-y to 2 decimal places)

wow i didnt even know there was a round function.  I am a computer science student so im learning haha. :)

but i cant get this one thing to work at all

Tohrchur

Link to comment
Share on other sites

Link to post
Share on other sites

Another tip: You have a lot of code that is very similar, why not reuse it? Programmers are lazy, so they shouldn't have to do work that's already been done.

 

Python makes it very easy to do iterations. For example:

 

You want to perform the same action for every unit of money there is. So start with a list of units of money:

 

 

>>> money_units = [10000, 5000, 2000, 1000, 500, 100, 25, 10, 5, 1] # python's 'list' type

>>> for unit in money_units:

                #Do stuff

I do not feel obliged to believe that the same God who has endowed us with sense, reason and intellect has intended us to forgo their use, and by some other means to give us knowledge which we can attain by them. - Galileo Galilei
Build Logs: Tophat (in progress), DNAF | Useful Links: How To: Choosing Your Storage Devices and Configuration, Case Study: RAID Tolerance to Failure, Reducing Single Points of Failure in Redundant Storage , Why Choose an SSD?, ZFS From A to Z (Eric1024), Advanced RAID: Survival Rates, Flashing LSI RAID Cards (alpenwasser), SAN and Storage Networking

Link to comment
Share on other sites

Link to post
Share on other sites

A better to do this is to convert both x and y into pennies individually before doing any arithmetic on them. Floating point values will lead to small errors

Link to comment
Share on other sites

Link to post
Share on other sites

Thank you for all the tips! :) 

The problem still persists though :( 

ive tried and tried but i cant get it to work :(

Tohrchur

Link to comment
Share on other sites

Link to post
Share on other sites

Thank you for all the tips! :)

The problem still persists though :(

ive tried and tried but i cant get it to work :(

Post your updated code.

I do not feel obliged to believe that the same God who has endowed us with sense, reason and intellect has intended us to forgo their use, and by some other means to give us knowledge which we can attain by them. - Galileo Galilei
Build Logs: Tophat (in progress), DNAF | Useful Links: How To: Choosing Your Storage Devices and Configuration, Case Study: RAID Tolerance to Failure, Reducing Single Points of Failure in Redundant Storage , Why Choose an SSD?, ZFS From A to Z (Eric1024), Advanced RAID: Survival Rates, Flashing LSI RAID Cards (alpenwasser), SAN and Storage Networking

Link to comment
Share on other sites

Link to post
Share on other sites

A better to do this is to convert both x and y into pennies individually before doing any arithmetic on them. Floating point values will lead to small errors

thats what multiplying by 100 does :)

 

Post your updated code.

I cant figure out how to get the list to work either because each variable (hundred, fifty,twenty, etc) changes so its not the same through out.

Tohrchur

Link to comment
Share on other sites

Link to post
Share on other sites

I cant figure out how to get the list to work either because each variable (hundred, fifty,twenty, etc) changes so its not the same through out.

That's the point. Look at your code:

 

 

if change>=10000:

        hundred=change/10000

        change=change-(10000*hundred)

        ...

    if change>=5000:

        fifty=change/5000

        change=change-(5000*fifty)

 

hundred is the number of hundred dollar bills you need, and fifty is the number of fifty dollar bills you need. Why not make the divisor (10000, 5000, etc) a variable, and calculate the number of units of money needed.

 

As far as the list goes:

 

 

>>> money_units = [10000, 5000, 2000, 1000, 500, 100, 25, 10, 5, 1]

>>> for unit in money_units:

              print "Unit of money is {}".format( unit )

'Unit of money is 10000'

'Unit of money is 5000'

'Unit of money is 2000'

'Unit of money is 1000'

'Unit of money is 500'

'Unit of money is 100'

'Unit of money is 25'

'Unit of money is 10'

'Unit of money is 5'

'Unit of money is 1'

Instead of printing out what the unit of money is (what I'm doing), you could instead calculate the quantity of that particular unit that you need to make change and print that.

 

The .format( unit ) is just so I can print a string and an integer easily in the same line.

I do not feel obliged to believe that the same God who has endowed us with sense, reason and intellect has intended us to forgo their use, and by some other means to give us knowledge which we can attain by them. - Galileo Galilei
Build Logs: Tophat (in progress), DNAF | Useful Links: How To: Choosing Your Storage Devices and Configuration, Case Study: RAID Tolerance to Failure, Reducing Single Points of Failure in Redundant Storage , Why Choose an SSD?, ZFS From A to Z (Eric1024), Advanced RAID: Survival Rates, Flashing LSI RAID Cards (alpenwasser), SAN and Storage Networking

Link to comment
Share on other sites

Link to post
Share on other sites

Just scanned over your original code and just want to point out why your program might not be working (I really need to learn python one of these days...but I think I spotted the problem)

 

So your code

    if change>=10000:        hundred=change/10000        change=change-(10000*hundred)        print hundred,"$100 bills"...print hundred,.....

If change lets say is 9999 then this if statement doesn't execute the code inside its block.  This is alright...until you hit this like

print hundred,fifty,twenty,ten,five,one,quarter,dime,nickel,penny

You are printing hundred....while this inherently looks good it has a flaw....what does hundred equal when change = 9999....hundred is never set so hundred does not exists...this is where my guess your program is failing...if you want to use your original code just add the following before your if statements (although as wpirobotbuilder said the number of lines can be reduced and reworked).

hundred=0fifty=0twenty=0ten=0five=0one=0quarter=0penny=0

0b10111010 10101101 11110000 00001101

Link to comment
Share on other sites

Link to post
Share on other sites

 

Just scanned over your original code and just want to point out why your program might not be working (I really need to learn python one of these days...but I think I spotted the problem)

 

So your code

    if change>=10000:        hundred=change/10000        change=change-(10000*hundred)        print hundred,"$100 bills"...print hundred,.....

If change lets say is 9999 then this if statement doesn't execute the code inside its block.  This is alright...until you hit this like

print hundred,fifty,twenty,ten,five,one,quarter,dime,nickel,penny

You are printing hundred....while this inherently looks good it has a flaw....what does hundred equal when change = 9999....hundred is never set so hundred does not exists...this is where my guess your program is failing...if you want to use your original code just add the following before your if statements (although as wpirobotbuilder said the number of lines can be reduced and reworked).

hundred=0fifty=0twenty=0ten=0five=0one=0quarter=0penny=0

aha! that helped a little. I knew that was the problem but did not know how to fix it.  That did indeed solve the error I was getting.  Now the program stops right after the hundred dollar bills.  

My idea is that if i did something like "variable=True" and then do "while variable==True:" then list the whole code and then after each set put "if change==0: variable=False" then it should stop right?

Tohrchur

Link to comment
Share on other sites

Link to post
Share on other sites

so would it be like "change/a=amount"....?

Correct. Then you reduce the amount of change remaining by that number times the current unit.

 

Try the loop, because it will make your program much more readable and easier to expand later on (if you wanted to start using a $2 bill for example.

I do not feel obliged to believe that the same God who has endowed us with sense, reason and intellect has intended us to forgo their use, and by some other means to give us knowledge which we can attain by them. - Galileo Galilei
Build Logs: Tophat (in progress), DNAF | Useful Links: How To: Choosing Your Storage Devices and Configuration, Case Study: RAID Tolerance to Failure, Reducing Single Points of Failure in Redundant Storage , Why Choose an SSD?, ZFS From A to Z (Eric1024), Advanced RAID: Survival Rates, Flashing LSI RAID Cards (alpenwasser), SAN and Storage Networking

Link to comment
Share on other sites

Link to post
Share on other sites

Correct. Then you reduce the amount of change remaining by that number times the current unit.

 

Try the loop, because it will make your program much more readable and easier to expand later on (if you wanted to start using a $2 bill for example.

Im sorry but im confused on how to set that up.  

Would I be doing change/a=amount (either number or variable?)

then say print (the variable ?)

also, could the list work like this:

a=[(list of money)]

then do change/a=(variable)

then do the subtraction to get new change.

and python would re do that with each new value in the list?

Tohrchur

Link to comment
Share on other sites

Link to post
Share on other sites

So using all of the tips besides the list because that still confuses me, the code works, perfectly :D. But I would love to understand the list and implement that.  :)

Tohrchur

Link to comment
Share on other sites

Link to post
Share on other sites

Here is the new code. 

 
 
def makeChange(x,y):
    hundred=0
    fifty=0
    twenty=0
    ten=0
    five=0
    one=0
    quarter=0
    penny=0
    variable=True
    change=round(y-x, 2)*100
    change=int(change)
    print change
    #money_units=[10000, 5000, 2000, 1000, 500, 100, 25, 10, 5, 1]
    #for units_money:
    if change>=10000:
        hundred=change/10000
        change=change-(10000*hundred)
        if change==0:
            variable=False
    if change>=5000:
        fifty=change/5000
        change=change-(5000*fifty)
        if change==0:
            variable=False
    if change>=2000:
        twenty=change/2000
        change=change-(2000*twenty)
        if change==0:
            variable=False
    if change>=1000:
        ten=change/1000
        change=change-(1000*ten)
        if change==0:
            variable=False
    if change>=500:
        five=change/500
        change=change-(500*five)
        if change==0:
            variable=False
    if change>=100:
        one=change/100
        change=change-(100*one)
        if change==0:
            variable=False
    if change>=25:
        quarter=change/25
        change=change-(25*quarter)
        if change==0:
            variable=False
    if change>=10:
        dime=change/10
        change=change-(10*dime)
        if change==0:
            variable=False
    if change>=05:
        nickel=change/05
        change=change-(05*nickel)
        if change==0:
            variable=False
    if change>=01:
        penny=change/01
        change=change-(01*penny)
        if change==0:
            variable=False
    print hundred,"$100 bills"
    print fifty,"$50 bills"
    print twenty,"$20 bills"
    print ten,"$10 bills"
    print five,"$5 bills"
    print one,"$1 bills"
    if change==0:
        #if hundred>1:
        #    print hundred, "hundred dollar bills"
        if quarter>1:
            print quarter, "quarters"
        if quarter==1:
            print quarter, "quarter"
        if dime>1:
            print dime, "dimes"
        if dime==1:
            print dime, "dime"
        if nickel>1:
            print nickel, "nickels"
        if nickel==1:
            print nickel, "nickel"
        if penny>1:
            print penny, "pennies"
        if penny==1:
            print penny, "penny"
makeChange(550.32,1000.00)

Tohrchur

Link to comment
Share on other sites

Link to post
Share on other sites

btw I forgot to mention, I missed nickel and dime :P

 

As for the list...the concept is like this

 

Your change is sitting at $123.45 (Your variable 12345)

So what your doing is constantly dividing by the amount, subtracting the total then printing out the name of it....this type of process can be greatly helped by using a list (or even a function)

 

I don't have time to go over the list example...but a quick way to clean up the code would be making a function which I think I could write quickly (excuse syntax and lack of compiling...general example)

def subtractAndChange(currentChange, moneyValue, moneyName, moneyNamePlural)	if currentChange < moneyValue:		return currentChange #there was none of this currency	amount=currentChange/moneyValue	if amount == 1:		print amount, moneyName	else		print amount, moneyNamePlural	return currentChange - (amount * moneyValue) #this basically contains your "if statement" in one small function which you can callchange=1010change = subtractAndChange(change, 100, "One dollar bill", "One dollar bills") #change should now be 10

Sorry out of time...but hope this gives some insight.

0b10111010 10101101 11110000 00001101

Link to comment
Share on other sites

Link to post
Share on other sites

Here is the most pythonic way of implementing this:

 

 

def makeChange(cost, paid):

    print "We must make ${} in change.".format(round(paid-cost,2))

    change = int(round(paid-cost,2) * 100)
    money_units = [10000, 5000, 2000, 1000, 500, 100, 25, 10, 5, 1] # python's 'list' type

    units_required = list()
    for unit in money_units:
           num_units = change/unit
           change = change - unit*num_units
           units_required.append(num_units)

 

    print "We need {} 100 dollar bills".format(units_required[0])  
    print "We need {} 50 dollar bills".format(units_required[1])
    print "We need {} 20 dollar bills".format(units_required[2])
    print "We need {} 10 dollar bills".format(units_required[3])
    print "We need {} 5 dollar bills".format(units_required[4])
    print "We need {} 1 dollar bills".format(units_required[5])
    print "We need {} quarters".format(units_required[6])
    print "We need {} dimes".format(units_required[7])
    print "We need {} nickels".format(units_required[8])
    print "We need {} pennies".format(units_required[9])

 

units_required stores the number of $100s, $50s, $20s, etc. that are required

I do not feel obliged to believe that the same God who has endowed us with sense, reason and intellect has intended us to forgo their use, and by some other means to give us knowledge which we can attain by them. - Galileo Galilei
Build Logs: Tophat (in progress), DNAF | Useful Links: How To: Choosing Your Storage Devices and Configuration, Case Study: RAID Tolerance to Failure, Reducing Single Points of Failure in Redundant Storage , Why Choose an SSD?, ZFS From A to Z (Eric1024), Advanced RAID: Survival Rates, Flashing LSI RAID Cards (alpenwasser), SAN and Storage Networking

Link to comment
Share on other sites

Link to post
Share on other sites

Here is the most pythonic way of implementing this:

 

 

units_required stores the number of $100s, $50s, $20s, etc. that are required

this is far beyond me haha. but it makes sense for the most part. Thank you so much! :D

Tohrchur

Link to comment
Share on other sites

Link to post
Share on other sites

I actually made something like this because it was in some list with fun projects, thought I'd share it. The code is not optimal by any means and probably contains some cringeworthy parts to people who have actual experience with python, and I did look at other solutions before coming up with my own, but here it is anyway:

 

http://pastebin.com/JFyiNUTm

| Operating Systems: Arch Linux  /  Debian Linux  /  Windows 7 | Audio: Epiphany Acoustics EHP-O2D + AKG Q701 |

| Display: Samsung Syncmaster P2450H | Mouse: Razer Deathadder Black | Keyboard: Filco Majestouch 2 Ninja TKL Brown | Mic: Samson C03U |

| Case: Corsair 600T | CPU: Intel Core i7 4770K | GPU: AMD HD7950 | Mobo: Gigabyte Z87X-UD4H | RAM: 16Gb Corsair Vengeance Pro | PSU: Corsair TX850M |

Link to comment
Share on other sites

Link to post
Share on other sites

Man, all those If statements...

If you manage to pull your code back into a manageable form without so many if statements, it will be much easier to debug and identify where you're going wrong.

Link to comment
Share on other sites

Link to post
Share on other sites

Man, all those If statements...

If you manage to pull your code back into a manageable form without so many if statements, it will be much easier to debug and identify where you're going wrong.

I got everything to work with someones help, the person that recommended that each variable be set to zero at first, that fixed everything :)

Tohrchur

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×