1
我有這個代碼的問題:調用初始化函數蟒蛇的內部方法
import math
class Money(object):
def __init__(self, salary):
self.salary = salary
sal(self.salary)
def sal(self, x):
y = (x - (((x * 0.22) + 6534)) - (1900.9408 + ((x - 37568)*.077)))
print '-----------------------------------------------------------'
print 'monthly income before tax will be: ${0:.2f}' .format(x/12)
print 'bi-weekly income before tax will be: ${0:.2f}' .format(x/24)
print 'Hourly after tax: ${0:.2f}' .format(x/24/70)
print '-----------------------------------------------------------'
print 'Income after tax will be: ${0:.2f}' .format(y)
print 'Monthly after tax: ${0:.2f}' .format((y/12))
print 'bi-weekly after tax: ${0:.2f}' .format((y/24))
print 'Hourly after tax: ${0:.2f}' .format(y/24/70)
answer = raw_input('Do you want to do this again?\nType [Y] or [N]: ')
if(answer == 'Y'):
sal(x)
else:
print 'Thank you!'
return
def main():
x = input('Enter your taxable income: ')
salaryLister = Money(x)
main()
回溯顯示了這個:
Traceback (most recent call last):
File "taxableincome.py", line 35, in <module>
main()
File "taxableincome.py", line 33, in main
salaryLister = Money(x)
File "taxableincome.py", line 7, in __init__
sal(self.salary)
NameError: global name 'sal' is not defined
是什麼: 全局名稱「SAL」是沒有定義的意思?
隨時對我的設計作出評論。我很想學習。