0

I am relatively new at coding so I don't know a lot about it. If you do manage to simplify, an explanation of why this works would be much appreciated.

So the code below is from a task I have for school. The task is to create a program which can output all the different ways to make £3.50 using change ranging from 1p - £2. If you can think of easier ways to do this please present them. I would like to see some ideas.

def change(n, coins_available, coins_so_far):
if sum(coins_so_far) == n:
    yield coins_so_far
elif sum(coins_so_far) > n:
    pass
elif coins_available == []:
    pass
else:
    for c in change(n, coins_available[:], coins_so_far+[coins_available[0]]):
        yield c
    for c in change(n, coins_available[1:], coins_so_far):
        yield c
n = 350
Usable_coins = print ("The coins you can use are 1p, 2p, 5p ,10p, 20p, 50p, £1 and £2")
amount = int(input("Please input how many coin types you would like to use"))

if amount == 1:
no1coin = input("Please input the coin you would like to use")
if no1coin == "1p" or "1":
    print ("350 X 1p")
elif no1coin == "2p" or "2":
    print ("175 X 2p")
elif no1coin == "5p" or "5":
    print("70 X 5p")
elif no1coin == "10p" or "10":
    print("35 X 10p")
elif no1coin == "20p" or "20":
    print("Not able to make £3.50")
elif no1coin == "50p" or "50":
    print("7 X50p")
elif no1coin == "£1" or "100":
    print("Not able to make £3.50")
elif no1coin == "£2" or "200":
    print("Not able to make £3.50")

elif amount == 2:
no1coin= int(input("Please input the first coin you would like to use"))
no2coin = int(input("Please input the second coin you would like to use"))
coins = [no1coin, no2coin]
solutions = [s for s in change (n, coins, [])]
for s in solutions:
    print (s)

elif amount == 3:
no1coin= int(input("Please input the first coin you would like to use"))
no2coin = int(input("Please input the second coin you would like to use"))
no3coin= int(input("Please input the third coin you would like to use"))
coins = [no1coin, no2coin, no3coin]
solutions = [s for s in change (n, coins, [])]
for s in solutions:
    print (s)

elif amount == 4:
no1coin= int(input("Please input the first coin you would like to use"))
no2coin = int(input("please input the second coin you would like to use"))
no3coin= int(input("Please input the third coin you would like to use"))
no4coin = int(input("please input the fourth coin you would like to use"))
coins = [no1coin, no2coin, no3coin, no4coin]
solutions = [s for s in change (n, coins, [])]
for s in solutions:
    print (s)

elif amount == 5:
no1coin= int(input("Please input the first coin you would like to use"))
no2coin = int(input("please input the second coin you would like to use"))
no3coin= int(input("Please input the third coin you would like to use"))
no4coin = int(input("please input the fourth coin you would like to use"))
no5coin= int(input("Please input the fifth coin you would like to use")
coins = [no1coin, no2coin, no3coin, no4coin, no5coin]
solutions = [s for s in change (n, coins, [])]
for s in solutions:
    print (s)

elif amount == 6:
no1coin= int(input("Please input the first coin you would like to use")
no2coin = int(input("please input the second coin you would like to use")
no3coin= int(input("Please input the third coin you would like to use")
no4coin = int(input("please input the fourth coin you would like to use")
no5coin= int(input("Please input the fifth coin you would like to use")
no6coin = int(input("please input the sixth coin you would like to use")
coins = [no1coin, no2coin, no3coin, no4coin, no5coin,no6coin]
solutions = [s for s in change (n, coins, [])]
for s in solutions:
    print (s)

elif amount == 7:
no1coin= int(input("Please input the first coin you would like to use")
no2coin = int(input("please input the second coin you would like to use")
no3coin= int(input("Please input the third coin you would like to use")
no4coin = int(input("please input the fourth coin you would like to use")
no5coin= int(input("Please input the fifth coin you would like to use")
no6coin = int(input("please input the sixth coin you would like to use")
no7coin= int(input("Please input the seventh coin you would like to use")
coins = [no1coin, no2coin, no3coin, no4coin, no5coin,no6coin,no7coin]
solutions = [s for s in change (n, coins, [])]
for s in solutions:
    print (s)

elif amount == 8:
print ("That is all the coins")
coins = [1, 2, 5, 10, 20, 50, 100, 200]
solutions = [s for s in change (n, coins, [])]
for s in solutions:
    print (s)
else:
print("This is not a valid input")
halfer
  • 19,824
  • 17
  • 99
  • 186
Archie.F
  • 9
  • 2
  • This is a classic problem in Dynamic Programming. It can be done in approx 5 lines of Python once you get the right idea. See for example: https://stackoverflow.com/a/41812422/674039 – wim Aug 25 '18 at 22:24
  • @wim I have tried to understand the post that you have linked me to but i do not understand it and therefore cannot implement it into my code. After all i only have a simple understanding of coding. Please could you explain it? – Archie.F Aug 26 '18 at 19:47
  • You should be aware that `no1coin == "1p" or "1"` doesn't do what you think it is doing. See https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-against-a-value – Patrick Haugh Aug 27 '18 at 14:48

0 Answers0