main()
, invoice.close()
, and the print function just above it all throw an "invalid syntax" exception.
I don't know anything about dictionaries or with functions (at this time), so this is the best i could come up with.
Here's my desired outcome:
Here are the contents of invoice.txt
:
#### CONTENTS OF invoice.txt ###
# hammer#9.95
# saw#20.15
# shovel#35.40
EDIT *** Here is the exception with the added bracket enter image description here
print('{0: <10}'.format('Item'), '{0: >17}'.format('Cost'), sep = '' )
def main():
invoice = open("invoice.txt", "r")
count = 0
total = 0
hammer = invoice.readline()
while invoice != "":
saw = invoice.readline()
shovel = invoice.readline()
hammer = hammer.rstrip("\n")
saw = saw.rstrip("\n")
shovel = shovel.rstrip("\n")
hammer = hammer.split("#")
saw = saw.split("#")
shovel = shovel.split("#")
print('{0: <10}'.format(hammer[0]), '{0: >17}'.format('$' + hammer[1]), sep = '' )
print('{0: <10}'.format(saw[0]), '{0: >17}'.format('$' + saw[1]), sep = '' )
print('{0: <10}'.format(shovel[0]), '{0: >17}'.format('$' + shovel[1]), sep = '' )
# total = total + float(hammer[1]) + float(saw[1]) + float(shovel[1]) # DOESN"T WORK
total = total + (int(float(hammer[1])) + int(float(saw[1])) + int(float(shovel[1]))
# total = total + (int(hammer[1])) + (int(saw[1])) + (int(shovel[1])) # DOESN"T WORK
print("{0: <10}".format("Total cost") + "{0: >17}".format("{0:.2f}".format(float(total))))
invoice.close()
main()
Assuming the contents are actually just
this is just a DSV using a
#
as the delimiter. Fortuantely, mostCSV
programs support multiple delimiters, including python's.