当我输入输出时,我没有得到正确的结果

时间:2013-05-30 02:28:51

标签: python-3.x

创建一个更改计数游戏,让用户输入正好一美元所需的硬币数量。 主要 这包含程序的主线逻辑。它执行以下操作:要求用户输入以下数字: o便士 o镍 o角钱 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()

2 个答案:

答案 0 :(得分:0)

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

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

  3. 你写pennies1, "dollars and", pennies1, "cents"这没有任何意义。

  4. 这应该按预期工作:

    #!/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")
    

    [edit]:如果您愿意,可以添加一些功能来解决问题:

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

答案 1 :(得分: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")