【发布时间】:2020-10-22 11:57:28
【问题描述】:
我编写了非常简单的 Python 代码来添加各种美元钞票或硬币,结果证明代码很长。请告诉我是否有任何改进,以便我可以提高效率。
#User input
a = int(input('Enter the number of hundreds: '))
b = int(input('Enter the number of fifties: '))
c = int(input('Enter the number of twenties: '))
d = int(input('Enter the number of tens: '))
e = int(input('Enter the number of fives: '))
f = int(input('Enter the number of one: '))
g = int(input('Enter the number of quarters: '))
h = int(input('Enter the number of dimes: '))
i = int(input('Enter the number of nickels: '))
j = int(input('Enter the number of pennies: '))
def hun(k):
return 100 * k
def fif(l):
return 50 * l
def twe(m):
return 20 * m
def ten(n):
return 10 * n
def fiv(o):
return 5 * o
def one(p):
return 1 * p
def qua(q):
return .25 * q
def dim(r):
return .10 * r
def nic(s):
return .05 * s
def pen(t):
return .01 * t
# Final computation
total = hun(a) + fif(b) + twe(c) + ten(d) + fiv(e) + one(f) + qua(g) + dim(h) + nic(i) + pen(j)
print('The total amount is $', total)
【问题讨论】:
-
有关改进工作代码风格的问题应发送至Code Review。但见codereview.meta.stackexchange.com/questions/5777/…
-
我投票结束这个问题,因为它不是试图解决一个特定的编程问题。正如@Barmar 所说,它更适合Code Review Stack Exchange。
标签: python python-3.x counting