python错误计数cs50 pset6现金

2024-04-19 01:53:43 发布

您现在位置:Python中文网/ 问答频道 /正文

硬币的数量不对

代码:

从cs50导入获取浮点数

将numpy作为np导入

尽管如此:

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

美分=美元*100

美分=圆形(美分,2)

硬币=0 面额=np.数组([25,10,5,1])

尺寸=长度(面额)

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

硬币=圆形(硬币)

印刷品(“,整数(硬币))


Tags: 代码numpyget数量np硬币圆形float
1条回答
网友
1楼 · 发布于 2024-04-19 01:53:43

哈哈

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)

相关问题 更多 >