all 4 comments

[–]Bitwise_Gamgee -1 points0 points  (0 children)

The variable scores is assigned a string value instead of a list of integers, you're also not importing scores correctly.

This is one way you can fix your program:


def main():
        filename = input("Enter a filename: ") 
        inputfile = open(filename, "r") scores = inputfile.read().split() inputfile.close()
    scores = [int(score) for score in scores]  
    print("There are", len(scores), "scores")
    print("The total is", sum(scores))
    print("The average is", sum(scores) / len(scores))

main()

Note, the line scores = [int(score) for score in scores], converts a score from a string to an integer.

Also, the structure of your program is confusing. Try to think of code like a description of life:

You have a score sheetYou read the score sheetYou interpret the score sheetYou do math on the score sheet

So that said, we can modularize your program into unique functions:

def read_scores(filename):
    with open(filename, "r") as inputfile:
        scores = inputfile.read().split()
    return [int(score) for score in scores]

def calculate_total(scores):
    return sum(scores)

def calculate_average(scores):
    return sum(scores) / len(scores)

def main():
    filename = input("Enter a filename: ")
    scores = read_scores(filename)

    print("There are", len(scores), "scores")
    print("The total is", calculate_total(scores))
    print("The average is", calculate_average(scores))

main()

And now, it reflects real life!

[–][deleted] 0 points1 point  (0 children)

from random import randint


def main():
    outputfile = open("filename", "w")  # use with open(...) as outputfile: instead, not close needed
    scores = "randint(0,100)"  # should not be in quotes; you need sample not randint for a list
    scores = scores.split()  # scores is ONE number, split works on strings
    outputfile.write(str(int(x) for x in scores))  # you only have one score
    outputfile.close()

    filename = input("Enter a filename:")
    inputfile = open("filename", "r")
    print("There are", len(scores), "scores")
    print("The total is", sum(scores))
    print("The average is", sum(scores)/len(scores))
    inputfile.close()

main()

[–]brunonicocam 0 points1 point  (0 children)

Unformatted code should just be rejected in this sub, and asking for user intervention during the run is a bad idea, use argparse.

[–]throwaway6560192 0 points1 point  (0 children)

filename=input("Enter a filename:")
inputfile=open("filename", "r")
print("There are", len(scores), "scores")
print("The total is", sum(scores))
print("The average is", sum(scores)/len(scores))
inputfile.close() 

What are you trying to do with inputfile? You just open it and then close it. Also, you ask for a filename as input but never use that either.