2017-09-17 129 views
-1

这是一个基本的数学问题。我想计算最低数量的纸币将被支付。Python如果条件为打印或不打印

这是我的代码,运行良好。

total_payment = int(input("Please enter the total amount: ")) 

Dollar50 = int(total_payment // 50) 
remaining_money = total_payment % 50 

Dollar20 = int(remaining_money // 20) 
remaining_money = remaining_money % 20 

Dollar10 = int(remaining_money // 10) 
remaining_money = remaining_money % 10 

Dollar5 = int(remaining_money // 5) 
remaining_money = remaining_money % 5 

Dollar1 = int(remaining_money // 1) 

print("We need {0} 50 dollar.".format(Dollar50)) 
print("We need {0} 20 dollar.".format(Dollar20)) 
print("We need {0} 10 dollar.".format(Dollar10)) 
print("We need {0} 5 dollar.".format(Dollar5)) 
print("We need {0} 1 dollar.".format(Dollar1)) 

但我想,只有在使用这种类型的钞票的印刷。例如,如果总量大于程序打印

We need 2 50 dollar. 
We need 0 20 dollar. 
We need 0 10 dollar. 
We need 0 5 dollar. 
We need 1 1 dollar. 

101美元,但我不想与0值打印的。我只希望它能打印

We need 2 50 dollar. 
We need 1 1 dollar. 

这是我奋斗的一个例子。我不能编码这种循环或条件。非常感谢您的帮助。

+0

“我不能编码这种类型的循环或条件”---首先,这里没有循环。其次,为什么不呢?当你尝试过任何错误?他们是什么? –

+0

我的问题是检查一个条件是否有价值。我试图编码像每个价值打印,如果他们有价值,但我不能。我不知道这种方式(如果Dollar20 :)意味着如果Dollar20有价值然后打印。 –

+0

你可以看到'bool(0)== False'。 '如果Dollar20'和'If Dollar20 == 0'一样 –

回答

0

所有你需要的是一个if条件。

作为练习,您应该尝试写DRY code。使用循环和列表看起来像这样

face_values = [50, 20, 10, 5, 1] 
amounts = [0]*5 
remaining_money = int(input("Please enter the total amount: ")) 

# While you have money left, calculate the next most 'dollar_value' you can use 
i = 0 # This is an index over the 'dollars' list 
while remaining_money > 0: 
    d = face_values[i] 
    amounts[i] = remaining_money // d 
    remaining_money %= d 
    i+=1 

denomination = "dollar bill" 
denomination_plural = denomination + "s" 

# Iterate both values and amounts together and print them 
for val, amount in zip(face_values, amounts): 
    if amount == 1: # Filter out any non-positive amounts so you don't show them 
    print("We need {} {} {}.".format(val, amount, denomination)) 
    else if amount > 1: 
    print("We need {} {} {}.".format(val, amount, denomination_plural)) 
+0

非常感谢。我是一个初学者,这种解决方法对我来说很重要,我猜想其他初学者也是如此。我们真的需要这种解决方案。 –

0

使用

if Dollar50: 
    print("We need {0} 50 dollar.".format(Dollar50)) 

如果值是0,也不会打印出任何东西。

+0

非常感谢你的工作。所以我改变我的代码\t如果Dollar50: \t \t print(“我们需要{0} 50美元。”。格式(Dollar50)) \t如果Dollar20: \t \t打印( “我们需要{0} 20美元的” 格式(Dollar20)) \t如果Dollar10: \t \t打印(“我们需要{0} 10 。美元 “格式(Dollar10)) \t如果Dollar5: \t \t打印(” 我们需要{0} 5美元 “格式(Dollar5)) \t如果Dollar1: \t \t打印(” 我们需要{0} 1美元。“格式(Dollar1)) –

+0

接受答案是什么,因为这是这里的大趋势。 :) – sprksh

+0

谢谢你的答案,但我接受上面的一个。 –

1

不是所有的这些只是zip他们一起写一个if声明和使用for循环:

counts = [Dollar50, Dollar20, Dollar10, Dollar5, Dollar1] 
ammounts = [50, 20, 10, 5, 1] 
for i, j in zip(counts, ammounts): 
    if i: 
     print("We need {} {} dollar.".format(i, j)) 
0

一种方式做,这将是使一个数组来存储所有的账单值遍历然而这将需要有点重写,使其与工作你有什么现在你需要的,如果树

例如

if Dollar50 > 0: 
    print("We need {0} 50 dollar.".format(Dollar50)) 

您可以向下继续这种逻辑,所有不同尺寸的法案

0

简单地来讲,只需要加一个如果围绕每一个print语句声明为0

if Dollar50 != 0: 
    print("We need {0} 50 dollar.".format(Dollar50)) 
if Dollar20 != 0: 
    print("We need {0} 20 dollar.".format(Dollar20)) 

继续检查,对每一个项目。