infilehandle = open ("receipts-10-28-13.txt", "r")
# FUNCTIONS
def stripsigns( astring ):
"""Remove dollar signs"""
signs = "$"
nosigns = ""
for numbers in astring:
if numbers not in signs:
nosigns = nosigns + numbers
return nosigns
def totaltake():
"""Calculate total take"""
total = 0
for line in infilehandle:
values = line.split(':')
cardnumbers = values[1]
cardnumbers = stripsigns(cardnumbers)
total = (total + eval(cardnumbers))
total = round(total,2)
return total
# more code etc
def computetax(rate, total):
total = totaltake() - totaltip()
taxed = total * rate
taxed = round(taxed,2)
return taxed
# more code etc
# VARS
total = totaltake()
tips = totaltip()
tax = computetax(rate,total)
rate = eval(input("Input the tax rate as an integer:"))
# PRINT
print("Total take: $", totaltake())
print("Total tips: $", totaltips())
print("Tax owed: $", computetax(rate,total))
我正在尝试创建一个文件,该文件将查看 txt 文件中的元素,然后根据文件中的数字进行计算。这些函数都是 totaltake() 的变体,它从文件中获取数字并找到总和,或者是 computetax(),它获取其他函数计算和乘/除/等的数字。我已经单独测试了所有功能并且它们都可以工作,但是当我尝试将它们放在一个文件中时,它并没有给我想要的输出,我不知道为什么。它打印 vars 列表中第一个的值,其他所有内容为 0 - 所以对于我上面的代码版本,它打印
Total take: $ 6533.47
Total tips: $ 0
Tax owed: $ 0
所以基本上,我做错了什么?