Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

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 March 15th, 2013, 06:26 AM
superflyish superflyish is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2013
Posts: 6 superflyish User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 42 m 52 sec
Reputation Power: 0
Help with code please

I have tried researching for this and I cannot find anything. If someone can help much appreciated.

5.21
4.63
5.24
3.62
1.98
16.47
1.31
1.85
4.26
0.98
1.84
0.51
15.58
2.64
4.32
0.59
0.21
5.41
0.08
4.34

Count the number of values in field [amount] more than or equal to ( 9.79)

If you can tell me the code to get the answer.

Thank you.

Reply With Quote
  #2  
Old March 15th, 2013, 09:10 AM
WynnDeezl WynnDeezl is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2011
Posts: 139 WynnDeezl User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 3 h 53 m 35 sec
Reputation Power: 2
For Loop

Code:
Cnt = 0

Values=open('filename','r').readlines()

for Value in Values :
  if int(Value) >= 9.79 : Cnt +=1

print Cnt

Reply With Quote
  #3  
Old March 15th, 2013, 09:44 AM
partoj partoj is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 138 partoj User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 11 h 39 m 41 sec
Reputation Power: 1
Quote:
Originally Posted by WynnDeezl
Code:
Cnt = 0

Values=open('filename','r').readlines()

for Value in Values :
  if int(Value) >= 9.79 : Cnt +=1

print Cnt


Have you tried your own program?

Code:
Traceback (most recent call last):
  File "count.py", line 6, in <module>
    if int(Value) >= 9.79 : Cnt +=1
ValueError: invalid literal for int() with base 10: '5.21\n'

Reply With Quote
  #4  
Old March 15th, 2013, 09:48 AM
partoj partoj is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 138 partoj User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 11 h 39 m 41 sec
Reputation Power: 1
Quote:
Originally Posted by superflyish
Count the number of values in field [amount] more than or equal to ( 9.79)


Based on WynnDeezl's example above:

Code:
#!/usr/bin/python                                                                                                                    

count = 0

with open('count', 'r') as f:
  values = f.readlines()

  for value in values:
    if float(value) >= 9.79:
      count += 1

print count

Reply With Quote
  #5  
Old March 15th, 2013, 03:28 PM
superflyish superflyish is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2013
Posts: 6 superflyish User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 42 m 52 sec
Reputation Power: 0
Thank you. I will try this as soon as my laptop is re-installed.

Reply With Quote
  #6  
Old March 15th, 2013, 03:41 PM
superflyish superflyish is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2013
Posts: 6 superflyish User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 42 m 52 sec
Reputation Power: 0
The code keeps coming with an error. I tried fixing the errors but they still don't work. I'll try explain the question again. I have got the code to open the file and read it.

The data is in an excel file.

amount (A1)
5.21 (A2)
4.63 (A3)
5.24 (A4)
3.62 (A5)
1.98 (A6)
16.47 (A7)
1.31 (A8)
1.85 (A9)
4.26 (A10)
0.98 (A11)
1.84 (A12)
0.51 (A13)
15.58 (A14)
2.64 (A15)
4.32 (A16)
0.59 (A17)
0.21 (A18)
5.41 (A19)
0.08 (A20)
4.34 (A21)

The question is:

Count the number of values in field [amount] more than or equal to ( 9.79)

Much appreciated for the help.

Thank you

Reply With Quote
  #7  
Old March 16th, 2013, 10:50 AM
partoj partoj is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 138 partoj User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 11 h 39 m 41 sec
Reputation Power: 1
Quote:
Originally Posted by superflyish
The code keeps coming with an error. I tried fixing the errors but they still don't work. I'll try explain the question again. I have got the code to open the file and read it.

The data is in an excel file.

amount (A1)
5.21 (A2)
[...]

The question is:

Count the number of values in field [amount] more than or equal to ( 9.79)

Much appreciated for the help.

Thank you


What error are you getting? What python version are you using?

If you have to interact with excel, I suggest you look at http://www.python-excel.org/ . If you can save the file as csv file, my code above should work.

Although if you have a heading in the file, change:

Code:
for value in values:


...to:

Code:
for value in values[1:]:

Reply With Quote
  #8  
Old March 16th, 2013, 11:27 AM
superflyish superflyish is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2013
Posts: 6 superflyish User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 42 m 52 sec
Reputation Power: 0
...

>...

Last edited by superflyish : March 16th, 2013 at 11:30 AM. Reason: Some details were wrong

Reply With Quote
  #9  
Old March 16th, 2013, 11:29 AM
superflyish superflyish is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2013
Posts: 6 superflyish User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 42 m 52 sec
Reputation Power: 0
I am using 3.2.3 I tried using your code but it kept coming with an error: ValueError: could not convert string to float: ' 5.21,I[N8U7]:75,184,Cold,2254,Cherry\n'

I changed the code to

import csv

with open('random.csv', 'r', newline='') as f:
reader = csv.reader(f)
next(reader)
count = 0
for row in reader:

amount = float(row[0])
if amount >= 9.79:
count += 1

print (count)


That code worked.

Reply With Quote
  #10  
Old March 17th, 2013, 07:10 AM
partoj partoj is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 138 partoj User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 11 h 39 m 41 sec
Reputation Power: 1
Quote:
' 5.21,I[N8U7]:75,184,Cold,2254,Cherry\n'


Ah.

If you had specified that the file contained more values than just the first one, it would've been much easier to help you.

Reply With Quote
  #11  
Old March 17th, 2013, 07:57 AM
superflyish superflyish is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2013
Posts: 6 superflyish User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 42 m 52 sec
Reputation Power: 0
Quote:
Originally Posted by partoj
Ah.

If you had specified that the file contained more values than just the first one, it would've been much easier to help you.


Sorry about that.

But thanks for the help.

Appreciate it.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Help with code please

Developer Shed Advertisers and Affiliates



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 - 2013, Jelsoft Enterprises Ltd.

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