2017-09-17 128 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)) 

繼續檢查,對每一個項目。