Как я могу упростить этот Python и есть ли более простой способ сделать это? - PullRequest
0 голосов
/ 26 августа 2018

Я относительно новичок в кодировании, поэтому я мало что знаю об этом. Если вам удастся упростить, объяснение того, почему это работает, будет высоко оценено.

Таким образом, приведенный ниже код взят из моего задания для школы. Задача состоит в том, чтобы создать программу, которая может выводить все различные способы заработать £ 3,50, используя изменения в диапазоне от 1p до £ 2. Если вы можете придумать более простые способы сделать это, пожалуйста, представьте их. Я хотел бы увидеть некоторые идеи.

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")
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...