我在编码方面比较陌生,所以我对此并不了解。如果您确实想简化,那么不妨解释一下其工作原理。
所以下面的代码来自我上学的一项任务。任务是创建一个程序,使用从1p到£2的变化,可以输出所有不同的方式来赚取£3.50。如果您能想到更简单的方法,请提出。我希望看到一些想法。
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")