Python inFile with loop -
i attempting open file python in order take averages off of file input.txt. after averages taken, must displayed after each person's name such as
<student>'s score <avg> bacons.
we have not yet gone on in class, i'm not sure i'm doing. assume we're supposed using loop, i'm not sure how implement it.
this code, keeps returning avg invalid syntax:
def main(): infile = open("input.txt","r") numvals = int(infile.readline()) name = infile.readline() number = float(int(infile.readline().split()) avg = float(int(number[0]* .3 + number[1]* .1 + number[2]* .6)) print("name[0 , len(name)]'s score is",avg,"bacons.") infile.close() main()
the input file contains
5 amelia 90 100 75 george 87 90 90 adam 100 80 85 jenny 75 80 90 carl 86 87 88
any help on appreciated. have no thought do.
your first problem line:
number = float(int(infile.readline().split())
you have 4 (
s, 3 )
s.
therefore, python expecting look go on on next line, sees =
, can't appear in middle of expression, raises syntaxerror
.
when syntaxerror
on line looks fine, there 2 things try.
first, break line smaller pieces, type smaller pieces interactive interpreter, etc.
second, 1 time you're sure line fine, @ line above , count parentheses (and brackets , braces). editor makes much easier spot—or type line interactive interpreter; if haven't closed parentheses, it'll give ...
instead of >>>
, meaning it's waiting finish previous line, instead of waiting whole new line.
once past that, you're going valueerror
, because you're trying phone call int
on list
of 3 strings got readline
. doesn't work; have phone call int
on each one. so, instead of this:
numbers = float(int(infile.readline().split()))
do this:
numbers = [float(int(number)) number in infile.readline().split()]
if haven't learned list comprehensions yet, you'll have loop instead. example:
numbers = infile.readline().split() n in range(len(numbers)): numbers[n] = float(int(numbers[n]))
by way, i'm not sure why you're doing float(int(…))
. if want truncate them integer values, don't need float
here—multiplying integer .3 works fine. if want maintain decimal part, don't need int
. so, utilize 1 or other.
and same thing on next line:
avg = float(int(number[0]* .3 + number[1]* .1 + number[2]* .6))
the result of number[0]* .3 + number[1]* .1 + number[2]* .6
float
, 89.1
george. convert int
, give 89
. convert float
, getting 89.0
. can't imagine want—you want un-converted 89.1
, or maybe truncated 89
int
, not 89.0
misleading float
.
also, way you're printing things out pretty strange, , i'm not sure it's supposed do:
print("name[0 , len(name)]'s score is",avg,"bacons.")
this print like:
name[0 , len(name)]'s score 89.0 bacons.
if want print value of variable, don't set in quotes:
print(name, "'s score is",avg,"bacons.")
but whatever you're trying 0
, len(name)
… well, don't know you're trying do, can't explain how it.
anyway, print:
george 's score 89.0 bacons.
you don't want space after george
, right? best way around utilize string formatting:
print("{}'s score {} bacons'".format(name, avg))
if haven't learned yet, honestly, think best thing deal space, or maybe rewrite sentence it's not problem:
print(name, 'has score of', avg, 'bacons.')
or, of course, larn before teacher gets it. it's not complicated.
finally, when past that, you're reading 1 name , list of numbers, not numvals
worth of them. that, need loop:
def main(): infile = open("input.txt","r") numvals = int(infile.readline()) in range(numvals): name = infile.readline() # ... rest of code happens each player # ... code happens after players, infile.close()
for-loop python-3.x average load-data-infile
No comments:
Post a Comment