【问题标题】:Im not getting the right results when i enter in my output当我输入输出时,我没有得到正确的结果
【发布时间】:2013-05-30 02:28:51
【问题描述】:

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

计算总美元金额并将其显示为便士。 当便士总数正好等于 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()

【问题讨论】:

  • 很确定你是 1.00 美元,而不是 1.25 美元。此外,您可能应该在 computeValue 函数中使用 100 进行比较,而不是 1。如果 pennies1 == 100,您有 1 美元。

标签: python-3.x


【解决方案1】:
  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")

【讨论】:

  • 谢谢。您将如何结合调用 computeValue 函数并将变量发送到参数?
  • @DaveLee 你想让computeValue 函数做什么?
【解决方案2】:

这是最简单的答案:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-01
    • 1970-01-01
    • 2023-01-30
    • 1970-01-01
    • 2017-01-03
    • 2020-03-21
    • 1970-01-01
    相关资源
    最近更新 更多