Python method for any integer returns a list consisting of numbers 5 and 7 only

First, you have an error in your code at line print (amount) because amount is not defined in the global scope, it is only defined as a local parameter of change() function. You should write print(change(24)) instead.

Secondly, your code manages only the input amount 24, nothing else. You have to design an algorithm to do the trick for any input number. With Python you can do it in a straightforward way to find a pair of numbers (i,j) for which i*5+j*7 == amount. You can check this equality for any pair value: i from 0 to amount, and j from 0 to amount, for example. Once this equality is found, then you can build the list with 5s and 7s and return.

Practically in Python:

  • to loop with i from 0 to amount-1: for i in range(amount):
  • to loop with j from 0 to amount-1: for j in range(amount):
  • to check the equality: if (i*5)+(j*7) == amount:
  • to build and return the list: return [5]*i + [7]*j

Obviously you have to learn the basics of Python to use this correctly.

Leave a Comment