-1

I'm trying to print the highest, average, and lowest score of a file in python. But I keep getting error

ValueError: invalid literal for int() with base 10.

My results.txt file looks like this:

Johnny-8.65
Juan-9.12
Joseph-8.45
Stacey-7.81
Aideen-8.05
Zack-7.21
Aaron-8.31

And my code looks like this

func1={}
with open('results.txt','r') as f:
    for line in f:
        name,value=line.split('-')
        value=float(value)
        if name in func1.keys():
           func1[name].append(value)
        else:
            func1[name]=[value]

#compute average: 
for name in func1:

    average=sum(func1[name])/len(func1[name])
    print("{} : {}".format(name,average))
CDspace
  • 2,639
  • 18
  • 30
  • 36
  • 1
    Yeah, `8.65` is not an int. It's a float. – Morgan Thrapp Sep 29 '16 at 15:50
  • 1
    `8.65` is not an int – Chris Taylor Sep 29 '16 at 15:50
  • I changed line 5 to float instead of value and i'm getting this error.Traceback (most recent call last): File "C:/Users/Trent/Desktop/CreativeProgrammingChapter4.py", line 5, in value=int(float)#or float TypeError: int() argument must be a string, a bytes-like object or a number, not 'type' >>> – Trenton Hallmark Sep 29 '16 at 15:51
  • Yeah, because `int(float)` tries to convert the `float` type object into an int. – Morgan Thrapp Sep 29 '16 at 15:53
  • No, they meant you should write `value=float(value)`... – Daneel Sep 29 '16 at 15:53
  • Ah okay, sorry guys i'm a beginner and this is for college homework. So, I did value=float(value) which makes sense and it's printing the names and the values but it isn't printing the highest, average, and lowest. – Trenton Hallmark Sep 29 '16 at 15:57
  • 1
    Several users have edited your question to try to make it more clear. Please take a look and make sure the edits haven't broken the formatting of your original code. In python, indentation is very important. If it doesn't look like your original code, remember that formatting code blocks requires an indentation of 4 spaces – CDspace Sep 29 '16 at 19:24

3 Answers3

0

Values that you provided in file are not int type. For your approach you can use float():

value = float(value)

If you need to extract integer:

s = "123"
num = int(s)

num = 123

If you need to extract float number:

s = "123.12"
num = float(s)

num = 123.12

You can refer to documentation: Datatypes #1

Also there is nice post about converting number datatypes in python: Parse String to Float or Int

How to get maximum key / value from dictionary refer to: get max valueget max key

Also, read this article. It will help you to ask clearer questions. How do I ask and answer homework questions?

Graham
  • 7,431
  • 18
  • 59
  • 84
Andrii Abramov
  • 10,019
  • 9
  • 74
  • 96
  • So I changed it to value=float() and it does print the names, but it's only printing 0.00 beside there name instead of actually printing the correct stuff? – Trenton Hallmark Sep 29 '16 at 15:55
  • No it hasn't, i've got it to print the numbers in the file but i'm having trouble getting it to print the highest and the lowest numbers as well as the average. How do I post my code in a comment so I can show you have i've got? – Trenton Hallmark Sep 29 '16 at 16:04
  • Alright i've edited my main question with the updated code that I have now. – Trenton Hallmark Sep 29 '16 at 16:07
0

As everyone on this thread suggested you should change int to float. Regarding your comment above. You use

value=int(float) ! 

Float should be outside parentheses. Like this

value=float(value).

I run the code and it worked for me.

semenoff
  • 63
  • 1
  • 1
  • 9
0

Try this:

scores={}
highest_score=0.0
highest=''
lowest_score=100.0
lowest=''
average=0.0
sums=0.0

files=open("results.txt","r").readlines()
for lines in files[0:]:
    line=lines.split("-")
    scores[line[0]]=line[1].strip()

for key,value in scores.items():
    if float(value)<lowest_score:
        lowest_score=float(value)
        lowest=key

    if float(value)>highest_score:
        highest_score=float(value)
        highest=key

    sums=sums+float(value)

print "highest score:",highest_score," of ",highest
print "lowest score:",lowest_score," of ",lowest
print "average: ",sums/len(scores)
Ranjana Ghimire
  • 1,785
  • 1
  • 12
  • 20