使用Python将美分转换为四分之一,镍,一角钱和几美分

问题描述 投票:2回答:3

我正在使用Python,我正在尝试将一定数量的钱(以美分表示)转换为等值的四分之一,镍,一角硬币和几美分。

这是我到目前为止所拥有的,但是我看到的问题是,我不知道如何从宿舍中拿走剩余的东西,将其分解为角钱,镍币和便士。我对此很陌生,很难过。我不是在要求别人解决问题,只是指出我做错了什么(也许我需要做些什么来解决它)。

# 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)
python converter
3个回答
3
投票

使用divmod,只有三行:

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

0
投票

这里需要两个操作:整数除法

整数除法A / B问一个简单的问题:B干净地适合A多少次(而不必将B分解成小数部分)? 2完全适合84次。 2也完全适合9 4次。

Modulo A % B提出了相同的问题,但给出了反面的答案:给定A干净地进入B几次,还剩下什么2干净地进入84次,没有剩余任何东西,因此2 % 802干净地进入9 4次,但是1剩下,所以2 % 91

我再举一个例子,让您过渡到自己的问题。假设我得到了多个seconds,我需要将其转换为dayshoursminutesseconds

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

0
投票

昨天晚上正在为此苦苦挣扎。确实,您需要除法和取模。这不是最Python的方式,但是它适用于任何金额,当您将可以输入自动售货机的金额限制为$ 5.00时,该金额就无效。这个问题被问到了,并一直被忽略。也许是因为它是家庭作业……无论如何。...


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
© www.soinside.com 2019 - 2024. All rights reserved.