-4

I am a beginner in python and need help to solve the error in my code. I have to write the code from the given flowchart such the user gets the minimum amount of coins in return, but I didn't get the accurate answer, so can you please help me to figure out my mistake and guide me. [1]: https://i.stack.imgur.com/AUq44.png
Here is my code:

print("amount: ",amount) 
payment = input("amount of Payment: ")
payment = float(payment) 
change = payment - amount 
change = round(change,2) 
if change>0: 
 d = change // 1 
 change = change - d 
  if change>0: 
   q = change // 0.25 
   i = change // 0.10 
   n = change // 0.05 
   change = change - q * 0.25 + i * 0.10 + n * 0.05 
   n = n+round(change*2,1)*10 
   print("you got",d,"dollars",q,"quaters,",i,"dimes, and",n,"nickels back in change") 
   else: print("you got",d,"dollars",q,"quaters,",i,"dimes, and",n,"nickels back in change")
else: print('No change owed')


  
RM6121
  • 1
  • 3
  • pls give `input` and expected `output` – jia Jimmy Feb 26 '21 at 08:51
  • You code has many mistakes, and you don't provide enough feedback regarding the initial values of your variables. Moreover, you have some syntax errors. I think that we won't be helping you if we simply re-write your code. Instead, you should study just a bit more about python, and them attempt to re-write your program. It's the right way, trust me! :-) – Alex Metsai Feb 26 '21 at 08:55
  • Also, I'm guessing that this is an assignment from your faculty, right? – Alex Metsai Feb 26 '21 at 08:56
  • @AlexMetsai yeah its an assignment – RM6121 Feb 26 '21 at 10:18

2 Answers2

1

I won't write the code for you as this will be the best way for you to take benefit from my answer and the help from the community, as well as respecing your work and your classmates'.

There are two main flaws which might cause the problems you are experiencing.

  1. You are calculating all the sub-unities of your currency based on the same value, i.e. the whole amount of change.

    q = change // 0.25 
    i = change // 0.10 
    n = change // 0.05 
    

The expected output of this section is to have, for a 2 dollars payment:

q = 8
i = 20
n = 40

But if we sum them up togheter, we obtain 8*0.25 + 20*0.1 + 40*0.05 = 6$! How's that?

Pointer: You need to make sure to update your change value, after you have performed the remainderless division.

  1. There is a chance that you get NameError: name 'q' is not defined

    if change>0: 
       q = change // 0.25 
       i = change // 0.10 
       n = change // 0.05 
       change = change - q * 0.25 + i * 0.10 + n * 0.05 
       n = n+round(change*2,1)*10 
       print("you got",d,"dollars",q,"quaters,",i,"dimes, and",n,"nickels       back in change") 
    else: 
       print("you got",d,"dollars",q,"quaters,",i,"dimes, and",n,"nickels back in change")
    

Taking a look at the above code, assume that change is equal to 0. You are however asking your Python script to print the value of d,q,i and n. But have you initialised these variables in this case?

BiOS
  • 2,282
  • 3
  • 10
  • 24
0

This is close to a manual division. That means that you should substract the big divisor part before using next divisor. You did int correctly for the dollar part (change = change -d), but failed to do it for the smaller coins. It should be:

if change>0: 
   q = change // 0.25
   change -= q * .25
   i = change // 0.10 
   change -= i * .10
   n = change // 0.05 
   change -= n * .25

Anyway, you are using floating point values for money operations, which is bad because floating point is inaccurate

As soon as you want exact operations in cents, you should either use integer number of cents, or use the Decimal class from the Standard Library.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252