Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming
Receive the tools necessary to be the rock star of your field. Our 12-month program teaches you the evolving world of multi-channel marketing as well as the complex issues and opportunities found in the industry.

ASP Free and Iron Speed Designer are giving away $5,500+ in FREE licenses. Iron Speed's RAD CASE toolset can save up to 80% of your coding time. One free license per week, one perpetual license per month!
Download and Activate to enter!

Web development can be a daunting task, even for specialists. There is a lot of information to absorb and a lot of technologies to learn in order to manage a superior website. When trying to learn the ropes, developers need a reliable source to introduce new ideas that can be easily implemented. When working on large projects, even web veterans may run into a technology or an aspect of a technology that they are unfamiliar with.

Learn More!


Download to Enter
| Contest Rules

Tutorials | Forums

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 8th, 2012, 09:39 PM
alohr51 alohr51 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2012
Posts: 2 alohr51 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 23 m 32 sec
Reputation Power: 0
Can you help me add certain numbers in a string?

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
  1.  
  2. import sys
  3. import string 
  4. def sumcosts (filename):
  5.       f = open(filename,'r')
  6.       toString = ','.join(f)
  7.       print toString
  8.  
  9.  
  10.  
  11. def main():
  12.       if len(sys.argv)>0:
  13.               sumcosts(sys.argv[1])
  14.  
  15. main()

Reply With Quote
  #2  
Old February 10th, 2012, 06:12 PM
sfb sfb is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Nov 2003
Posts: 623 sfb User rank is Sergeant Major (2000 - 5000 Reputation Level)sfb User rank is Sergeant Major (2000 - 5000 Reputation Level)sfb User rank is Sergeant Major (2000 - 5000 Reputation Level)sfb User rank is Sergeant Major (2000 - 5000 Reputation Level)sfb User rank is Sergeant Major (2000 - 5000 Reputation Level)sfb User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Days 5 h 53 m 21 sec
Reputation Power: 32
Well, this is useful: "are separated from other text by whitespace" because you can use toString.split() to split the string into pieces where there is whitespace, and go through each piece to see if it's a price or not.

So, something like:

Code:
for chunk in toString.split():
    Does chunk have numbers in it?
    does chunk have a $ at the start? If so, get rid of it
    If chunk has a . at the end, get rid of it.
    Does chunk have other text like "kgs" or "lbs" in it?
    If all of the above means it's a price, convert it from text to a number, and add it to your running total, otherwise ignore it.

print the finished total

Reply With Quote
  #3  
Old February 12th, 2012, 06:42 PM
alohr51 alohr51 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2012
Posts: 2 alohr51 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 23 m 32 sec
Reputation Power: 0
Thanks, I appreciate your help

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Can you help me add certain numbers in a string?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.

© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 5 - Follow our Sitemap