【问题标题】:Convert cents to quarters, nickels, dimes and pennies using Python使用 Python 将美分转换为 25 美分、5 美分、1 美分和美分
【发布时间】:2015-10-21 02:41:42
【问题描述】:

我正在使用 Python,我正在尝试将一定数量的美分货币转换为等值的 25 美分硬币、五分硬币和一美分硬币。

这是我目前所拥有的,但我发现问题是我不知道如何从硬币中取出剩余的部分并将其分解为一角硬币、五分钱和便士。我是新手,只是遇到了困难。我不是要找人来解决问题,只是指出我做错了什么(也许我需要做些什么来解决它)。

# 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)

【问题讨论】:

  • 您计算了cents % quarter,但没有将其分配回下一条语句的变量。根据你所拥有的,你可以做cents = cents % quarterscents % 语句的其余部分同上。
  • 您也可以使用divmod

标签: python converter


【解决方案1】:

使用divmod,只需三行:

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

【讨论】:

    【解决方案2】:

    这里需要两个运算:整数除法取模

    整数除法A / B 提出了一个简单的问题:B 可以干净地融入A 多少次(无需将B 分解为小数部分)? 2 完全适合 8 4 次。 2 也适合 9 干净地 4 次。

    Modulo A % B 提出了同样的问题,但给出了相反的答案:鉴于 A 干净利落地进入 B 若干次,还剩下什么2 干净利落地进入 8 4 次,没有任何剩余,所以 2 % 802 干净利落地进入 9 4 次,但留下了 1,所以 2 % 91

    我会给你另一个例子,让你从那个例子过渡到你的问题。假设我有一个 ,我需要将其转换为 小时分钟

    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
    

    【讨论】:

    • 感谢 dogwynn 的帮助,您的解释恰到好处
    • 很好,我很高兴。祝你好运,@DerekMcFarland。
    【解决方案3】:

    昨天晚上为此苦苦挣扎。没错,你需要除法和模数。不是最 Pythonic 的方式,但它适用于任何金额,当您将可以输入自动售货机的美元金额限制为 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
    

    【讨论】:

      猜你喜欢
      • 2021-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-08
      • 2015-08-23
      • 1970-01-01
      • 2017-06-05
      • 1970-01-01
      相关资源
      最近更新 更多