-1

I am a beginner at python. I have a code that reads a text file from my computer and converts all the text to int. I am struggling with the last few lines in the analyze golf scores function.

I need to tell the code that for a score under 280 to take these values and get the amount, which I know to use Len(score) for but I am going wrong with getting the values in the first place. It should print the number of scores below 180, but I keep getting errors and I am so lost! Any help is appreciated at all! Thank you so much!!!

My error is in the last six lines of code! I don't know how to get it to read the values under 280 in the list :(

The error is:

TypeError: '<' not supported between instances of 'str' and 'int'##

on the line:

    if score < 280:
def open_and_read_golf_scores():
    raw_scores = open("golfscores.txt", "r")
    scores = []

    for current_line in raw_scores:
        values = current_line.split(",")
        scores.append(values[2])

    raw_scores.close()

    return scores

def analyze_golf_scores():

    scores = open_and_read_golf_scores()
    total = 0
    for score in scores:
        score = score[0:3]
        total = total +int(score)

    ave = total/len(score)

print("Average score =", ave)

for score in scores:
    if score < 280:
        scores.append(values)
        below_par = total + len(score)
print("The number of scores below par is ", below_par)
6
  • Please read How to Ask and minimal reproducible example Commented Dec 1, 2021 at 15:00
  • Your indentation looks wrong -- it's not clear if there's a copy+paste error or if that's the source of the problems you're hitting. Please review that and edit your question to include the exact code you're running and the exact errors you're receiving. Commented Dec 1, 2021 at 15:02
  • I have edited the code x Commented Dec 1, 2021 at 15:04
  • Now there's not enough code to run -- it's not clear what scores or values are. And you still haven't included your error. :( Commented Dec 1, 2021 at 15:06
  • hold on, ill fix it my bad Commented Dec 1, 2021 at 15:11

1 Answer 1

0

This code looks problematic:

    for score in scores:
        score = score[0:3]
        total = total +int(score)

        ave = total/len(score)

    print("Average score =", ave)

    for score in scores:
        if score < 280:
            scores.append(values)
            below_par = total + len(score)
    print("The number of scores below par is ", below_par)
  • The thing you're computing as ave is not the numeric average of the scores, it's the total divided by the number of digits in each score (which is 3). Is that what you meant to compute? Did you mean to divide by len(scores) instead?
  • Your scores are strings, and you're trying to compare them to the number 280, which is why you're getting that TypeError. You should convert them to int as you did above, or better yet, have open_and_read_golf_scores return them as ints in the first place.
  • Right after where you hit that error, you try to do something with values, which is unbound in this scope. Maybe you meant to use score?

A lot of problems will probably go away if your open_and_read_golf_scores function just returns the scores as ints:

from typing import List

def open_and_read_golf_scores() -> List[int]:
    with open("golfscores.txt", "r") as raw_scores:
        return [
            int(current_line.split(",")[2][:3]) 
            for current_line in raw_scores
        ]

Note: I kept the logic of slicing each score to its first three characters, but I have no idea why that's necessary because I can't see the contents of the file -- it feels like it might actually lead to a bug. Is every score really exactly three digits, with appropriate zero-padding? Is there some extra stuff after the score? What is that stuff? Could there be an safer way to get rid of it than making the assumption that the number is always 3 digits long?

Now your analyze_golf_scores function can be much simpler, because you can do basic mathematical operations on scores without having to convert each item in a loop:

from statistics import mean


def analyze_golf_scores() -> None:
    scores = open_and_read_golf_scores()
    ave = mean(scores)
    below_par = sum(score < 280 for score in scores)

    print(f"Average score = {ave}")
    print(f"The number of scores below par is {below_par}")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.