0

创建一个找零游戏,让用户输入恰好赚一美元所需的硬币数量。main 这包含程序的主线逻辑。它执行以下操作: 要求用户输入以下数字: o Pennies o Nickels o Dimes o Quarters 将用户输入值转换为 int 值。调用 computeValue 函数并将这 4 个变量作为参数发送。

计算总美元金额并将其显示为便士。当便士总数正好等于 1 美元时,打印出一条消息。如果计算值小于 1 美元,则显示金额并显示该值小于 1 美元的消息,并打印该金额 如果计算机值大于 1 美元,则分别显示美元金额和美分。有关示例,请参见示例输出。

我的样本由 5、3、3、2 组成,我想得到“我有 1 美元和 25 美分”,而不是我有 5 美元和 5 美分。

def main():
    pennies=int(input("Enter the Number of pennies : "))
    nickels = int(input("Enter the Number of nickels : "))
    dimes = int(input("Enter the Number of dimes : "))
    quarters =int(input("Enter the Number of quarters : "))

    pennies1=pennies*1
    nickels1=nickels*5
    dimes1=dimes*10
    quarters1=quarters*25

    total=pennies1 +nickels1+dimes1+quarters1
    cash=total//100
    change=total-cash*100
    computeValue(pennies1,nickels1,dimes1,quarters1)

def computeValue(pennies1,nickels1,dimes1,quarters1):
    print("You entered")
    if (pennies1 == 1):
        print ("You have exactely 1 dollar")
    elif (pennies1 < 1):
        print ("You have",pennies1,"which is less than 1 dollar")
    elif (pennies1 > 1):
        print ("pennies:",pennies1,"\nnickels:",nickels1,"\ndimes:",dimes1,
        "\nquarters:",quarters1,"\nYou have",pennies1,"dollars and",pennies1,"cents")

main()
4

2 回答 2

0
  1. 您的程序对如何计算该值非常困惑。

  2. 5*1 + 2*5 + 2*10 + 2*25 = 100, 不是125

  3. 你写pennies1, "dollars and", pennies1, "cents"的没有意义。

这应该按预期工作:

#!/usr/bin/env python


if __name__ == "__main__":
    pennies = int(input("Enter the Number of pennies : "))
    nickels = int(input("Enter the Number of nickels : "))
    dimes = int(input("Enter the Number of dimes : "))
    quarters = int(input("Enter the Number of quarters : "))

    cents = pennies
    cents += nickels*5
    cents += dimes*10
    cents += quarters*25

    dollars = int(cents / 100) 
    cents = cents % 100 

    print ("You entered {} pennies {} nickels {} dimes and {} quarters".format(pennies, nickels, dimes, quarters))
    print ("You have {} dollars and {} cents".format(dollars,cents))

    if dollars == 1 and cents == 0:
      print("You Win")
    else:
      print("you fail")

[编辑]:如果需要,您可以添加一些功能来分解:

def change_to_cents(pennies, nickels, dimes, quarters):
    cents = pennies
    cents += nickels*5
    cents += dimes*10
    cents += quarters*25
    return cents

def display_change(pennies, nickels, dimes, quarters):
    print ("You entered {} pennies {} nickels {} dimes and {} quarters".format(pennies, nickels, dimes, quarters))

def display_cents(cents):
    dollars = int(cents / 100) 
    cents = cents % 100
    print ("You have {} dollars and {} cents".format(dollars,cents))

if __name__ == "__main__":
    pennies = int(input("Enter the Number of pennies : "))
    nickels = int(input("Enter the Number of nickels : "))
    dimes = int(input("Enter the Number of dimes : "))
    quarters = int(input("Enter the Number of quarters : "))

    display_change(pennies, nickels, dimes, quarters)        

    cents = change_to_cents(pennies, nickels, dimes, quarters)
    display_cents(cents)

    if cents == 100:
        print("You Win")
    else:
        print("you fail")
于 2013-05-30T02:50:46.490 回答
0

这是最简单的答案:

penny = int(input('Enter the number of pennies: '))
nickel = int(input('Enter the number of nickels: '))
dime = int(input('Enter the number of dimes: '))
quarters = int(input('Enter the number of quarters: '))
PENNIES = 100
NICKELS = 20
DIMES = 10
QUARTERS = 4
dollar = (penny / PENNIES) + (nickel / NICKELS) + (dime / DIMES) + (quarters / QUARTERS)
if dollar == 1:
    print("Congratulations! You've entered one dollar!")
elif dollar > 1:
    print("Sorry! You've entered more than one dollar")
else:
    print("Sorry! You've entered less than one dollar")
于 2019-09-23T19:38:07.023 回答