user2962562 user2962562 - 3 months ago 9
Python Question

Usage of map function correctly with an if statement for python

I tried to use map correctly and used and if statement to make sure that if the list was empty to not proceed and stop. I will display the input as well. For clarification, numbers_1 function is where i used the map option. What would i need to edit to make this work? I'm confused on how to fix this my code is below is my code

#this is the input file
#John Jackson
#91 94 38 48 70 85 94 59
#James Johnson
#78 96 90 55 77 82 94 60
#Edward Kinsley
#99 94 82 77 75 89 94 93
#Mozilla Firefox
#49 92 75 48 80 95 99 98
def lab8():
userinput= "Lab8.txt"
lenoffile= len(userinput)
print "There is", lenoffile, "lines"
File= open (userinput, "r")
studentscores1= File.read()
studentlist= studentscores1.split("\n")
return studentlist, lenoffile
def Names_1(studentlist, lenoffile):
print "=============================="
ai = ""
for i in range (0, lenoffile, 2):
ai += studentlist[i] + "\n"
print "===============below is ai=========="
print ai
return ai
def Numbers_1(studentlist, lenoffile):
bi= ""
for i in range (1, lenoffile, 2):
bi += studentlist[i] + "\n"
bi = bi.split ("\n")
print bi
return bi
print "====================BELOW IS THE SCORE========================="
def Outputfile_1(ai):
outputfile= raw_input ("What is the output file.txt:")
File2= open(outputfile, "w")
File2.write(ai)
return outputfile

def numbers_1(bi):
for b1 in bi:
b1 = b1.split(" ")
lenofb1 = len(b1)
quiztotalb = 0
midtermb = 0
Final = 0
if lenofb1 > 0:
b1 = map(int, b1)
quiztotal = ((b1[0] + b1[1] + b1[2] + b1[3] + b1[4])/5)
midtermtotal = ((b1[5]) + b1[6])/2
Finaltotal = (b1[7])
Score = (quiztotal*.3 + midtermtotal*.4 + Finaltotal*.3)
print Score
def main():
studentlist, lenoffile = lab8()
ai = Names_1(studentlist, lenoffile)
bi = Numbers_1(studentlist, lenoffile)
#outputfile = Outputfile_1(ai)
numbers_1(bi)
main()


from this i get the
ValueError: invalid literal for int() with base 10: ''

I have been trying really hard and I'm not sure where I should go from here.

Answer

You are splitting b1 on single spaces, and this can lead to empty values:

>>> '88  89 '.split(' ')
['88', '', '89', '']

It is the extra empty strings here that cause int() to throw an exception:

>>> int('')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: ''

Use str.split() with no argument instead; extra whitespace is then stripped:

>>> '88  89 '.split()
['88', '89']

You have some other problems as well in your code. Take a good look at:

def lab8():
    userinput= "Lab8.txt"
    lenoffile= len(userinput)
    print "There is", lenoffile, "lines"
    File= open (userinput, "r")
    studentscores1= File.read()
    studentlist= studentscores1.split("\n")
    return studentlist, lenoffile

Here, lenoffile is not the number of lines in the file. It is the number of characters in 'Lab8.txt'; both values happen to be 8, but add or remove some lines from that file and the number will be wrong for the rest of your code.

If you are supposed to keep these numbers together with the names and write out the calculations again, you'll have to do some work keeping the names together.

Here is an alternative version to solve the same task:

outputfile = raw_input("What is the output filename? :")

with open('Lab8.txt') as infile, open(outputfile, 'w') as outtfile:
    for name in infile:
        scores = next(infile).split()  # next() grabs the next line from infile here
        scores = map(int, scores)

        quiztotal = sum(scores[:4]) / 5
        midtermtotal = sum(scores[5:7]) / 2
        finaltotal = scores[7]
        score = quiztotal * .3 + midtermtotal * .4 + finaltotal * .3

        outfile.write(name)
        outfile.write('{0:0.2f}\n'.format(score))
Comments