Hello, my assignment for class is to write a program that opens a .txt file and locates the prices in the .txt file then sums them.
for example,
This is your invoice from the ACME materials
company. You received 50lbs of sand at a
cost of $40. The brick we delivered is 70.5
for the 75Kg. In addition, we delivered 30yards
of sod for $200.00. Delivery charge is $35.
--In the above file the prices are 40, 70.5, 200.00, and 35. The 50lbs, 75kg, and 30yards are
not prices.
The rules for a price are as follows
Prices
• may be integers or floating points (may contain an internal decimal point)
• are separated from other text by whitespace
• may contain a trailing period (if they occur at the end of a sentence)
• may (or may not) contain a leading dollar sign ($)
So far i got it to open any file I want and turn it into one big string. Now i do not know how to traverse the string and pick out only the prices and add them up. Any help is appreciated thanks.
Here is what i have so far
python Code:
Original
- python Code |
|
|
|
import sys
import string
def sumcosts (filename):
f = open(filename,'r')
toString = ','.join(f)
print toString
def main():
if len(sys.argv)>0:
sumcosts(sys.argv[1])
main()