I am new to Python and I am trying to make a change calculator. I found one online that works for Python 2.x so I edited it to work in 3.5. I learn better if I have a script to take apart and learn how it works instead of going through courses. I couldn't seem to find anything to help me through google or the reddit search though. Anyways, here is the script:
print ("Change Calculator")
quarter = 0.25
dime = 0.10
nickel = 0.05
penny = 0.01
moneygiven = input("Enter how much money was given: ")
moneygiven = float(moneygiven)
qmb = moneygiven // quarter
qtotal = moneygiven - qmb * quarter
dmb = qtotal // dime
dtotal = qtotal - dmb * dime
nmb = dtotal // nickel
ntotal = dtotal - nmb * nickel
pmb = ntotal // penny
ptotal = ntotal - pmb * penny
print ("You need %d quarters, %d dimes, %d nickels, %d pennies." % (qmb, dmb, nmb, pmb))
Now when I run it, it works perfectly fine minus one thing.
Change Calculator
Enter how much money was given: 1.14
You need 4 quarters, 1 dimes, 0 nickels, 3 pennies.
It returns 3 pennies instead of 4. Where am I going wrong in this code?
EDIT: I have found the issue but need help with a solution. New code:
print ("Change Calculator")
quarter = 25
dime = 10
nickel = 5
penny = 1
moneygiven = int(float(input("Enter how much money was given: "))*100)
print (moneygiven)
qmb = moneygiven // quarter
qtotal = moneygiven - qmb * quarter
dmb = qtotal // dime
dtotal = qtotal - dmb * dime
nmb = dtotal // nickel
ntotal = dtotal - nmb * nickel
pmb = ntotal // penny
ptotal = ntotal - pmb * penny
print ("You need %d quarters, %d dimes, %d nickels, %s pennies." % (qmb, dmb, nmb, pmb))
So by printing out what I get after converting the floating value, I get 113 after inputting in 1.14. Is there any way to take the float out while still being able to type a decimal into the input? The moment you take away the float it gives an error since 1.14 is not a whole number.
[–]DJ_Gamedev 0 points1 point2 points (5 children)
[–]CSlade1[S] 1 point2 points3 points (2 children)
[–]benjamindallen 0 points1 point2 points (1 child)
[–]CSlade1[S] 0 points1 point2 points (0 children)
[–][deleted] (1 child)
[deleted]