#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    4
    Rep Power
    0

    Help with a very difficult Python homework assignment, due tonight!


    Hi. My name is Devin. I am studying at the University of New Mexico. I am in a program sponsored by SONY Studios that teaches students how to create CGI effects for film or video games. One of the core classes that is part of the program is a newly added class called "intro to computer programming using Python". A lot of us are struggling because most of us (especially me) have never done any coding before. I am having a really rough time with this class and could use some help from someone on this board.

    We have a homework assignment. let me post it so you all will know what it is:

    Listing 3.4 of the textbook, ComputeChange.py, accepts a dollar and cents amount of money and outputs a multiset (a set that can contain more than one copy of an element) of coins consisting of pennies, nickels, dimes, quarters, and Sacagawea dollars with minimal cardinality.
    Your mission is to modify this program as follows:
    1) Your program must input two numbers: the total cost of a purchase and the amount of money paid. Both inputs are in British pounds with two decimal places.
    2) Outputs the multiset of coins with minimal cardinality that is equal in value to the change due using the eight dominations pictured above. Note: in the modern British system, 1 pound equals 100 pence.

    The first line your programís output is exactly:
    Your total change is: £□□□□.□□ (end)

    Ok. so basically we are trying to make a program that asks for the cost of an item...then asks how much you paid...then computes the change you should receive, not in US Currency, but in British Currency. Here is the code I have written so far:

    # Receive the amount
    amount = float(input('Enter the cost of the item: £'))

    total = float (input ('Enter the amount paid: £') )
    change = int(paid * 100 - total * 100)

    multiset = []
    for nomination in [200, 100, 50, 20, 10, 5, 2, 1]:
    count = change // nomination
    change = change % nomination
    for _ in range (count): multiset.append (nomination)

    print (', '.join ('{:.2f}'.format (nomination / 100) for nomination in multiset) )
    print ('Total change: £{:>4.2f}'.format (paid - total) )

    # convert the amount to pence
    remainingamount = int(amount * 100)

    # find the number of two pounds
    numberOfTwoPounds = remainingAmount // 200
    remainingAmount = remainingAmount % 200

    # find the number of one pound
    numberOfOnePounds = remainingAmount // 100
    remainingAmount = remainingAmount % 100

    # find the number of fifty pence
    numberOfFiftyPence = remainingAmount // 50
    remainingAmount = remainingAmount % 50

    # find the number of twenty pence
    numberOfTwentyPence = remainingAmount // 20
    remainingAmount = remainingAmount % 20

    # find the number of ten pence
    numberOfTenPence = remainingAmount // 10
    remainingAmount = remainingAmount % 10

    # find the number of five pence
    numberOfFivePence = remainingAmount // 5
    remainingAmount = remainingAmount % 5

    # find the number of two pence in the remaining amount
    numberOfTwoPence = remainingAmount // 2
    remainingAmount = remainingAmount % 2

    # find the number of pennies in the remaining amount
    numberOfPennies = remainingAmount


    Ok, so when I run this code I get a box asking me to enter the cost of the item. I put in the number 11.56 (the box asks me this question using a British pound symbol)

    After I hit enter, a new box asks me to enter the amount paid in British pounds...I put 20.00.

    At this point I get an error box that says "NameError: name 'paid' is not defined"...the red highlighted error is the following line of code:
    change = int(paid * 100 - total * 100)

    So. my question is what am I doing wrong and what do I need to change in order for this code to work properly. I want to make clear that I am not trying to get anyone to necessarily "do my homework for me" I just really need some guidance and help on what the problem is, and how to fix it. I am sorry this post is so long, I am just trying to explain this as thoroughly as I can. The assignment is due tonight by midnight. If someone could help me out, I would really appreciate it. Thank You
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    Your program starts out like this:

    amount = float(input('Enter the cost of the item: £'))
    total = float (input ('Enter the amount paid: £') )


    The variables "amount" and "total" now have definitions. Then you proceed to write change in terms of "paid" and "total".

    change = int(paid * 100 - total * 100)


    Incidentally, divmod returns the quotient and remainder of integer division all at once.

    (quotient, remainder,) = divmod(17, 8)
    # quotient stores 2
    # remainder stores 1


    There you go! You're ready to be the lead programmer for WOW 6.
    Last edited by b49P23TIvg; September 11th, 2013 at 02:44 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    4
    Rep Power
    0

    My code re-done, but still having a problem


    Ok, thanks for the help. I have re-done my code and am no longer getting any errors. However, here is what is happening now. box says enter the purchase amount-I put in 11.56. next box says: enter amount paid-I put in 20.00.
    then the program calculates the change, but the change it gives me back is 20.00 (all of this is in British pounds)

    So there is still something I am not doing right. I am not sure if it is another line of code I have left out or not?

    Any help with this part would be really appreciated.

    here is my re-done code as it stands now:

    # Receive the amount
    amount = float(input('Enter the cost of the item: £'))

    paid = float (input ('Enter the amount paid: £') )
    change = int(amount * 100 - paid * 100)

    multiset = []
    for nomination in [200, 100, 50, 20, 10, 5, 2, 1]:
    count = change // nomination
    change = change % nomination
    for _ in range (count): multiset.append (nomination)

    print (', '.join ('{:.2f}'.format (nomination / 100) for nomination in multiset) )
    print ('change: £{:>4.2f}'.format (paid - change) )

    # convert the amount to pence
    remainingAmount = int(amount * 100)

    # find the number of two pounds
    numberOfTwoPounds = remainingAmount // 200
    remainingAmount = remainingAmount % 200

    # find the number of one pound
    numberOfOnePounds = remainingAmount // 100
    remainingAmount = remainingAmount % 100

    # find the number of fifty pence
    numberOfFiftyPence = remainingAmount // 50
    remainingAmount = remainingAmount % 50

    # find the number of twenty pence
    numberOfTwentyPence = remainingAmount // 20
    remainingAmount = remainingAmount % 20

    # find the number of ten pence
    numberOfTenPence = remainingAmount // 10
    remainingAmount = remainingAmount % 10

    # find the number of five pence
    numberOfFivePence = remainingAmount // 5
    remainingAmount = remainingAmount % 5

    # find the number of two pence in the remaining amount
    numberOfTwoPence = remainingAmount // 2
    remainingAmount = remainingAmount % 2

    # find the number of pennies in the remaining amount
    numberOfPennies = remainingAmount
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    Follow the guide at my signature to post python code.
    Code:
    amount = 11.56 or float(input('Enter the cost of the item: '))  # fixed the input to 11.56
    paid = 20 or float (input ('Enter the amount paid: ') )  # assigned input of 20
    change = abs(int(round(100*(amount - paid))))    # was   change = int(amount * 100 - paid * 100)
    
    multiset = []
    for nomination in [200, 100, 50, 20, 10, 5, 2, 1]:
        ##### here you alter the value stored in variable named "change'
        count, change = divmod(change, nomination) # was // and %
        multiset.extend([nomination]*count)  # was for _ in range(count):multiset.append(nomination)
    
    print(', '.join('{:.2f}'.format(nomination/100.0)for nomination in multiset)) # 100 changed to 100.0 floating point computation required.
    
    ################ good through here.
    
    
    
    # you've changed the value of your variable change
    # it's zero here.
    # and that's why it always shows 20
    
    print ('change: {:>4.2f}'.format (paid - change) )
    why is there additional code?
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    4
    Rep Power
    0

    Why additional code


    Well, here is the thing. I posted my original homework assignment on another website and somebody posted a code for me to try. however it didn't work, so we modified it a bunch. here is the part of the code that we have not messed with, and I don't know enough about coding to know what any of it means.

    code not touched:

    multiset = []
    for nomination in [200, 100, 50, 20, 10, 5, 2, 1]:
    count = change // nomination
    change = change % nomination
    for _ in range (count): multiset.append (nomination)

    print (', '.join ('{:.2f}'.format (nomination / 100) for nomination in multiset) )
    print ('change: £{:>4.2f}'.format (paid - change) )

    Ok, so is this part of the code causing my problem? Do I need to take it out or change it in any way? Thanks for trying to help by the way
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    cost - paid
    is usually a negative value.
    And since the mod operation seems to be defined differently per computer language, i use positive values.
    Your program is sort of close. I'd probably grade it C work before looking at the other students' responses.
    Code:
    import sys
    
    def input(prompt = ''): # avoid version trouble
        sys.stdout.write(prompt)
        sys.stdout.flush()
        answer = sys.stdin.readline()
        if answer and (answer[-1] == '\n'): # remove new line
            s = slice(0,-1)
        else:
            s = slice(0,len(answer))
        return answer[s]
    
    # Receive the amount
    cost = 4.31 or float(input('Enter the cost of the item: £'))
    paid = 5 or float(input('Enter the amount paid: £') )
    
    # convert the quantity of change to pence
    change_in_pence = int(round((paid-cost) * 100))
    
    # find the number of two pounds
    (numberOfTwoPounds, remaining_change_in_pence) = divmod(
        change_in_pence, 200)
    
    # find the number of one pound
    (numberOfOnePounds, remaining_change_in_pence) = divmod(
        remaining_change_in_pence, 100)
    
    # find the number of fifty pence
    (numberOfFiftyPence, remaining_change_in_pence) = divmod(
        remaining_change_in_pence, 50)
    
    # find the number of twenty pence
    (numberOfTwentyPence, remaining_change_in_pence) = divmod(
        remaining_change_in_pence, 20)
    
    # find the number of ten pence
    (numberOfTenPence, remaining_change_in_pence) = divmod(
        remaining_change_in_pence, 10)
    
    # find the number of five pence
    (numberOfFivePence, remaining_change_in_pence) = divmod(
        remaining_change_in_pence, 5)
    
    # find the number of two pence in the remaining amount
    (numberOfTwoPence, remaining_change_in_pence) = divmod(
        remaining_change_in_pence, 2)
    
    numberOfPennies = remaining_change_in_pence
    
    print(vars())
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo