简体   繁体   中英

python wrong count cs50 pset6 cash

The count of the coins is not right

code:

from cs50 import get_float

import numpy as np

while True:

dollar = get_float("change owed: ")
if dollar >= 0:
    break

cents = dollar * 100

cents = round(cents, 2)

coins = 0 denominations = np.array([25, 10, 5, 1])

size = len(denominations)

for i in range(size):
 coins += cents / denominations[i]
 cents %= denominations[i]

coins = round(coins)

print("", int(coins))

LOL

from cs50 import get_float
import numpy as np

#prompt the user for change in dollar
while True:
    dollar = get_float("change owed: ")
#break the loop if the given input is valid
    if dollar >= 0:
        break
#convert the given input(dollar) to cents
cents = dollar * 100
#rounding the cents to two decimal
cents = round(cents, 2)
#creating a variable for counting the coins
coins = 0
#creating an array for quarter, dime, nickel, penny
denominators = np.array([25, 10, 5, 1])
#substracting the cents by the array and count the coins
for i  in range(len(denominators)):
    while cents>=denominators[i]:
        cents -= denominators[i]
        coins +=1
print("", coins)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM