首页 > 解决方案 > 我怎样才能简化这个 Python,有没有更简单的方法来做到这一点?

问题描述

我在编码方面相对较新,所以我不太了解它。如果您确实设法简化,将不胜感激解释为什么这样做。

所以下面的代码来自我的学校任务。任务是创建一个程序,该程序可以输出所有不同的方式,使用从 1 便士到 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")

标签: python-3.7

解决方案


推荐阅读