我正在尝试学习如何创建函数。如何将此代码更改为多个函数?
purchase = input('Enter the amount of purchase: ')
statetaxes = purchase * 0.05
countytaxes = purchase * 0.025
totaltaxes = (statetaxes + countytaxes)
totalPurchase = (purchase + totaltaxes)
print('The amount of purchase is $'), format(purchase, ',.2f')
print('State tax: $'), format(statetaxes, ',.2f')
print('County tax: $'), format(countytaxes, ',.2f')
print('Total tax: $'), format(totaltaxes, ',.2f')
print('Total: $'), format(totalPurchase, ',.2f')
会是这样的:
def main():
purchase = get_purchase
statetaxes = get_state
countytaxes = get_county
totaltaxes = statetaxes + countytaxes
totalPurchase = totaltaxes + purchase
print('The amount of purchase is $', purchase)
print('State tax: ', statetaxes)
print('County tax: ', countytaxes)
print('Total tax: ', totaltaxes)
print('Total: $'. totalPurchase)
def get_purchase():
purchase = float(input('Please enter the amount of purchase')
return purchase
def get_state():
state = purchase * 0.05
return statetaxes
def get_county():
countytaxes = purchase * 0.025
return countytaxes
main()
这是对的吗?如果没有,我哪里错了?
我这样做没有python翻译,因为我现在正在使用平板电脑等待航班。
编辑:我要做的是将顶级程序分成多个功能。当我输入此代码时:
def get_purchase():
return float(input('Please enter the amount of purchase '))
def get_state():
return purchase * 0.05
def get_county():
return purchase * 0.025
def main():
purchase = get_purchase()
statetaxes = get_state()
countytaxes = get_county()
totaltaxes = statetaxes + countytaxes
totalPurchase = totaltaxes + purchase
print('The amount of purchase is $', purchase)
print('State tax: ', statetaxes)
print('County tax: ', countytaxes)
print('Total tax: ', totaltaxes)
print('Total: $'. totalPurchase)
main()
我收到此错误:
Please enter the amount of purchase 5000
Traceback (most recent call last):
File "salestax.py", line 49, in <module>
main()
File "salestax.py", line 38, in main
statetaxes = get_state()
File "salestax.py", line 27, in get_state
return purchase * 0.05
NameError: name 'purchase' is not defined
我现在坐飞机,但会在停留时检查,看看我做错了什么。
答案 0 :(得分:2)
您需要调用您的函数,方法是添加左/右括号:
purchase = get_purchase()
在get_state()
中,您有state
和statetaxes
个变量 - 您只想使用其中一个。
此外,作为mhawke mentioned,您需要get_state
和get_county
获取purchase
参数,传递从主要商品购买在你打电话时起作用。
除此之外,您的功能似乎正确地分离了逻辑操作。
答案 1 :(得分:0)
代码审核:
将main()代码移到底部。
def get_purchase():
'''Use docstrings to describe the function'''
# purchase = float(input('Please enter the amount of purchase')
# return purchase # no need to assign and then return the assignment
# just return what you'd otherwise assign:
return float(input('Please enter the amount of purchase')) # was missing a )
def get_state():
'''calculate and return state taxes'''
# state = purchase * 0.05
# return statetaxes # see a problem here? what is statetaxes?
return purchase * 0.05 # there, it's fixed!
def get_county():
# countytaxes = purchase * 0.025
# return countytaxes
return purchase * 0.025
所以我们将你的main()移到了底部。
def main():
purchase = get_purchase() # now we're calling these with the ()'s
statetaxes = get_state()
countytaxes = get_county()
totaltaxes = statetaxes + countytaxes
totalPurchase = totaltaxes + purchase
print('The amount of purchase is $', purchase)
print('State tax: ', statetaxes)
print('County tax: ', countytaxes)
print('Total tax: ', totaltaxes)
print('Total: $'. totalPurchase)
最后,在模块的底部,不要只是调用main()
,使用常见的Python习语:
if __name__ == '__main__':
main()
如果导入模块,它会阻止代码执行main()。导入模块时,其__name__
不再是'__main__'
相关:What does if __name__ == "__main__": do?
答案 2 :(得分:0)
这个答案是在您编辑的地方进行的,您已经更改了代码并且现在正在获得运行时异常。
函数def get_state()
和get_county()
都引用名为purchase
的变量,该变量不在函数范围内。
您应该将purchase
传递给每个函数,如下所示:
def get_state(purchase):
return purchase * 0.05
def get_county(purchase):
return purchase * 0.025
def main():
purchase = get_purchase()
statetaxes = get_state(purchase)
countytaxes = get_county(purchase)
还有其他方法可以做到这一点,例如:基于类的方法,但是当你说你正在学习函数时,这可能是要走的路。