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

New Free Tools on Dev Shed!

#1
March 15th, 2013, 07:26 AM
 superflyish
Registered User

Join Date: Mar 2013
Posts: 6
Time spent in forums: 1 h 42 m 52 sec
Reputation Power: 0

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.

#2
March 15th, 2013, 10:10 AM
 WynnDeezl
Contributing User

Join Date: Aug 2011
Posts: 139
Time spent in forums: 2 Days 3 h 53 m 35 sec
Reputation Power: 3
For Loop

Code:
```Cnt = 0

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

print Cnt```

#3
March 15th, 2013, 10:44 AM
 partoj
Contributing User

Join Date: Feb 2013
Posts: 138
Time spent in forums: 1 Day 12 h 26 m 47 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'```

#4
March 15th, 2013, 10:48 AM
 partoj
Contributing User

Join Date: Feb 2013
Posts: 138
Time spent in forums: 1 Day 12 h 26 m 47 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:

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

print count```

#5
March 15th, 2013, 04:28 PM
 superflyish
Registered User

Join Date: Mar 2013
Posts: 6
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.

#6
March 15th, 2013, 04:41 PM
 superflyish
Registered User

Join Date: Mar 2013
Posts: 6
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

#7
March 16th, 2013, 11:50 AM
 partoj
Contributing User

Join Date: Feb 2013
Posts: 138
Time spent in forums: 1 Day 12 h 26 m 47 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:]:`

#8
March 16th, 2013, 12:27 PM
 superflyish
Registered User

Join Date: Mar 2013
Posts: 6
Time spent in forums: 1 h 42 m 52 sec
Reputation Power: 0
...

>...

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

#9
March 16th, 2013, 12:29 PM
 superflyish
Registered User

Join Date: Mar 2013
Posts: 6
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:
count = 0

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

print (count)

That code worked.

#10
March 17th, 2013, 08:10 AM
 partoj
Contributing User

Join Date: Feb 2013
Posts: 138
Time spent in forums: 1 Day 12 h 26 m 47 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.

#11
March 17th, 2013, 08:57 AM
 superflyish
Registered User

Join Date: Mar 2013
Posts: 6
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.

But thanks for the help.

Appreciate it.

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > Help with code please