python-3.7 - 我怎样才能简化这个 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")
解决方案
推荐阅读
- keycloak - 在 Keycloak 中以编程方式创建组后使缓存无效
- html - Materialise 和 Datetime Picker 相互交互
- javascript - 按下回车键时执行操作
- python - Selenium (Python) 模式匹配
- npm - 使用 yarn 工作空间时,如何强制将包安装在相关的 node_modules 中?
- c# - c# ninject 在实例化类中注入依赖项
- c++ - 从函数返回静态固定大小的数组
- linux-kernel - 内核如何跟踪映射到其他进程的帧?
- gridfs - 无法使用 gridfs 将文件存储到 mongodb Atlas
- php - 使用 php 从 URL 获取解码 json 以显示最新版本的 fontawesome