-1

Here is a basic math problem. I want to calculate least amount of banknotes to be paid.

Here is my code and working well.

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

But i want to print only if that type of banknote is used. For example if total amount is 101 dollar than program prints

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.

But i dont want to print the ones with 0 value. I only want it to print

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

This was an example of my struggle. I can not code this kind of loops or conditions. Thank you very much for any help.

Nevzat Korkut
  • 45
  • 2
  • 8
  • "I can not code this kind of loops or conditions" --- First, there are no loops here. Second, why not? Any errors when you tried? What were they? – OneCricketeer Sep 17 '17 at 21:46
  • The problem i had was to check if a condition has a value or not. I tried to code like for every value print if they had value but i couldn't. I didn't know this way (if Dollar20:) means if Dollar20 has a value then print. – Nevzat Korkut Sep 17 '17 at 21:58
  • You can see that `bool(0) == False`. `if Dollar20` is the same as `if Dollar20 == 0` – OneCricketeer Sep 17 '17 at 22:18
  • So after i saw your great answer below i wanted to ask what sources would you suggest the beginners like me to find this kind of examples and solutions to examine them. Thank you very very much. – Nevzat Korkut Sep 17 '17 at 22:25
  • The official website is fine. https://docs.python.org/3/tutorial/ – OneCricketeer Sep 17 '17 at 22:25

4 Answers4

1

Instead of writing an if statement for all of these just zip them together and use a for loop:

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))
Dimitris Fasarakis Hilliard
  • 150,925
  • 31
  • 268
  • 253
0

Use

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

if the value is 0, it won't print anything.

sprksh
  • 2,204
  • 2
  • 26
  • 43
  • Thank you very much it worked. So i changed my code to if Dollar50: print("We need {0} 50 dollar.".format(Dollar50)) if Dollar20: print("We need {0} 20 dollar.".format(Dollar20)) if Dollar10: print("We need {0} 10 dollar.".format(Dollar10)) if Dollar5: print("We need {0} 5 dollar.".format(Dollar5)) if Dollar1: print("We need {0} 1 dollar.".format(Dollar1)) – Nevzat Korkut Sep 17 '17 at 21:52
  • what about accepting the answer as it's the general trend here. :) – sprksh Sep 17 '17 at 22:06
  • Thank you for the answer but i accept the upper one. – Nevzat Korkut Sep 17 '17 at 22:20
0

Speaking simplistically, just add an if statement around each print statement checking for a 0.

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

Continue that for each item.

Danny Flack
  • 126
  • 6
0

All you need is an if condition.

As an exercise, you should try to write DRY code. Using loops and lists would look like this

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))
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Thank you very much. I am a beginner and this kind of solution ways teach a lot to me and i guess to other beginners. We really need this sort of solutions. – Nevzat Korkut Sep 17 '17 at 22:00