代码:
def main():
incomefile = open(r'C:Usersjaceyincome.txt', 'w')
income1 = int(input("Please enter your current income:"))
tax1= float(input("please enter current tax:"))
income2 = int(input("Please enter your current income:"))
tax2= float(input("please enter current tax:"))
income3 = int(input("Please enter your current income:"))
tax3= float(input("please enter current tax:"))
income4 = int(input("Please enter your current income:"))
tax4= float(input("please enter current tax:"))
grossIncome= (income1+income2+income3+income4, '.2f')
taxTotal= (tax1+tax2+tax3+tax4, '.2f')
grossTaxes = (grossIncome*taxTotal, '.2f')
netIncome = (grossIncome - grossTaxes, '.2f')
incomefile.write(str(income1) +'n')
incomefile.write(str(tax1) +'n')
incomefile.write(str(income2) +'n')
incomefile.write(str(tax2) +'n')
incomefile.write(str(income3) +'n')
incomefile.write(str(tax3) +'n')
incomefile.write(str(income4) +'n')
incomefile.write(str(tax4) +'n')
incomefile.write(str(grossIncome) +'n')
incomefile.close()
print('Data recorded in income.txt')
main()
grossTaxes = (grossIncome*taxTotal)
错误:
TypeError: can't multiply sequence by non-int of type 'tuple'
执行此操作时:
grossIncome= (income1+income2+income3+income4, '.2f')
taxTotal= (tax1+tax2+tax3+tax4, '.2f')
它创建元组:
元组可以通过多种方式构建:
- 使用一对圆括号表示空元组:((
- 对单个元组使用尾部逗号:a或(a,(
- 用逗号分隔项目:a、b、c或(a、b和c(
- 使用内置的元组((:元组((或元组(可迭代(
然后这个:
grossTaxes = (grossIncome*taxTotal)
正在尝试将这些元组相乘。这行不通。
你想用语法(val+val2+val3...., '.2f')
做什么?只需将值相加,去掉'.2f'
和括号:tax=val1+val2+val3....