Python如果条件为打印或不打印

Python如果条件为打印或不打印,python,python-3.x,if-statement,Python,Python 3.x,If Statement,这是一道基本的数学题。我想计算最少要付的钞票金额 这是我的代码,运行良好 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

这是一道基本的数学题。我想计算最少要付的钞票金额

这是我的代码,运行良好

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))
但我只想在使用那种钞票的情况下打印。例如,如果总金额比程序打印金额高101美元

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.
但是我不想打印那些值为0的。我只想把它打印出来

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

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

使用

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

如果值为0,则不会打印任何内容。

而不是为所有这些内容编写
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))

简单地说,只需在每个print语句周围添加一个if语句来检查0

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

对每一项都继续这样做。

您所需要的只是一个if条件

作为练习,你应该试着写作。使用循环和列表将如下所示

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

“我不能对这种循环或条件进行编码”——首先,这里没有循环。第二,为什么不呢?你试的时候有错误吗?它们是什么?我的问题是检查一个条件是否有值。我试图为每一个有价值的印刷品编码,如果它们有价值的话,但我做不到。我不知道这种方式(如果Dollar20:)意味着如果Dollar20有一个值,那么打印。您可以看到
bool(0)==False
if Dollar20
与if Dollar20==0相同,因此在我看到下面的精彩答案后,我想问一下,您建议像我这样的初学者寻找此类示例和解决方案来检查它们的来源是什么。非常感谢。官方网站很好。非常感谢,它成功了。所以我把代码改为if Dollar50:print(“我们需要{0}50美元。”.format(Dollar50))if Dollar20:print(“我们需要{0}20美元。”.format(Dollar20))if Dollar10:print(“我们需要{0}10美元。”.format(Dollar10))if Dollar5:print(“我们需要{0}5美元。”.format(Dollar5))if Dollar1:print(“我们需要{0}1美元。”.format(Dollar1))接受答案怎么样,因为这是这里的大趋势。:)谢谢你的回答,但我接受上面的答案。非常感谢。我是一个初学者,这种解决方法对我和其他初学者都有很大的帮助。我们确实需要这种解决办法。