Having trouble using if-else statements to print number of 100, 50, 10, and 1 rupee notes in Python 3.x

192 views Asked by At

Write a program that reads an amount A and prints the minimum number of 100, 50, 10 and 1 rupee notes required for the given amount. Output: The first line of output should be a string containing the required number of 100 rupee notes as shown in the sample output. The second line of output should be a string containing the required number of 50 rupee notes as shown in the sample output. The third line of output should be a string containing the required number of 10 rupee notes as shown in the sample output. The fourth line of output should be a string containing the required number of 1 rupee notes as shown in the sample output.

I tried it 3 times , and I am now unable to do it using the if else statements , please anyone help

2

There are 2 answers

0
Shivam Pandey On

Here is a solution to your question:

  def find_min_notes(amount):
  notes100 = amount // 100
  amount %= 100
  notes50 = amount // 50
  amount %= 50
  notes10 = amount // 10
  amount %= 10
  notes1 = amount

  return notes100, notes50, notes10, notes1


def main():
  amount = int(input("Enter the amount: "))

  notes100, notes50, notes10, notes1 = find_min_notes(amount)

  print("100 rupee notes:", notes100)
  print("50 rupee notes:", notes50)
  print("10 rupee notes:", notes10)
  print("1 rupee notes:", notes1)


if __name__ == "__main__":
  main()
0
Matej On

My first idea is

note_values = [100, 50, 10, 1]

u_amount = input("Amount of rupee: ")

try:
    total = int(u_amount)
except ValueError:
    print("User input failed to convert to integer.")

for note_value in sorted(note_values, reverse=True):
    note_amount = total // note_value
    total %= note_value
    print(f"Amount of {note_value} rupee notes is {note_amount}")

I'm pretty sure there is a simpler way, but at least this works.

Example:

Amount of rupee: 6581
Amount of 100 rupee notes is 65
Amount of 50 rupee notes is 1
Amount of 10 rupee notes is 3
Amount of 1 rupee notes is 1