使用Python将美分转换为25美分、5美分、1美分和1美分

2024-10-03 11:23:49 发布

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

我正在使用Python,我试图将一定数量的以美分为单位的货币转换成25美分、5美分、1美分和1美分的等值货币。在

这是我目前所拥有的,但我发现问题是我不知道如何从硬币中取出剩余的硬币,然后把它分解成一角硬币、五分镍币和一分硬币。我是新手,只是日子不好过。我不是要求别人来解决问题,只要指出我做错了什么(也许我需要做些什么来解决它)。在

# Convert some money to an appropriate collection of cents
penny = 1
nickel = 5
dime = 10
quarter = 25

quarters = 0
dimes = 0
nickels = 0
pennys = 0

cents = int(input("Please enter an amount of money you have in cents: "))

if cents >= 25:
    quarters = cents / quarter
    cents % quarter
if cents >= 10:
    dimes = cents/dime
    cents % dime
if cents >= 5:
    nickels = cents /nickel
    cents % nickel
if cents > 0:
    pennys = cents / penny
    cents = 0

print ("The coins are: quarters", quarters,\
",dimes", dimes, ",nickels", nickels, ", and pennys.", pennys)

Tags: ofanif货币硬币moneydimequarter
3条回答

这里需要两个运算:整数除法。在

整型除法A / B提出了一个简单的问题:B将干净地放入A中多少次(而不必将B拆分成小数部分)?2适合8干净4次。2也可以干净地放入9次。在

A % B提出了相同的问题,但给出了相反的答案:假设A干净地进入B若干次,剩下的是什么?2干净地进入8次,没有留下任何东西,所以2 % 8是{}。2干净地进入9次,但1次,所以2 % 9是{}。在

我再给你举一个例子,让你从这个问题过渡到你的问题。假设给了我一些,我需要把它转换成小时分钟。在

total_seconds = 345169

# Specify conversion between seconds and minutes, hours and days
seconds_per_minute = 60
seconds_per_hour = 3600 # (60 * 60)
seconds_per_day = 86400 # (3600 * 24)

# First, we pull out the day-sized chunks of seconds from the total
# number of seconds
days = total_seconds / seconds_per_day
# days = total_seconds // seconds_per_day # Python3

# Then we use the modulo (or remainder) operation to get the number of
# seconds left over after removing the day-sized chunks
seconds_left_over = total_seconds % seconds_per_day

# Next we pull out the hour-sized chunks of seconds from the number of
# seconds left over from removing the day-sized chunks
hours = seconds_left_over / seconds_per_hour
# hours = seconds // seconds_per_hour # Python3

# Use modulo to find out how many seconds are left after pulling out
# hours
seconds_left_over = seconds_left_over % seconds_per_hour

# Pull out the minute-sized chunks
minutes = seconds_left_over / seconds_per_minute
# minutes = seconds_left_over // seconds_per_minute # Python3

# Find out how many seconds are left
seconds_left_over = seconds_left_over % seconds_per_minute

# Because we've removed all the days, hours and minutes, all we have
# left over are seconds
seconds = seconds_left_over

昨天晚上一直在挣扎。是的,你需要除法和模。不是最像Python的方式,但它对任何数量都有效,当你把你可以输入自动售货机的美元金额限制在5美元时。这个问题一直被问到,却一直被忽视。也许是因为它是家庭作业。。。不管怎样。。。。在


def vending_machine_change():
    cost = float(input("Enter cost of item: "))
    change= 5.00-cost
    dollars = int(change)
    quarters_change= float("%.2f" % ((change-dollars)))
    quarters =int(quarters_change/0.25)
    dime_change= float("%.2f" % (quarters_change%0.25))
    dimes=int(dime_change/0.10)
    nickel_change = float("%.2f" % (dime_change%0.10))
    nickels= int(nickel_change/0.05)
    pennys = int(nickel_change*100)
    print("Change amount: " + str((dollars)) + ' Dollars, ' + str((quarters)) + ' Quarters, '+ str((dimes)) + ' Dimes, '+ str((nickels)) + ' Nickels, '+ str((pennys)) + ' Pennies' )
    pass

使用divmod,它只有三行:

quarters, cents = divmod(cents, 25)
dimes, cents = divmod(cents, 10)
nickels, pennies = divmod(cents, 5)

相关问题 更多 >