在python的if语句中正确使用map函数

在python的if语句中正确使用map函数,python,function,map-function,Python,Function,Map Function,我试图正确地使用map,并使用and if语句来确保如果列表为空,则不会继续和停止。我也会显示输入。为了澄清,numbers_1函数是我使用map选项的地方。我需要编辑什么才能使它工作?我对如何解决这个问题感到困惑。下面是我的代码 #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 7

我试图正确地使用map,并使用and if语句来确保如果列表为空,则不会继续和停止。我也会显示输入。为了澄清,numbers_1函数是我使用map选项的地方。我需要编辑什么才能使它工作?我对如何解决这个问题感到困惑。下面是我的代码

#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()
从中我得到
ValueError:invalid literal for int(),基数为10:“

我一直在非常努力地尝试,我不确定我应该从这里走到哪里。

您正在单个空格上拆分
b1
,这可能会导致空值:

>>> '88  89 '.split(' ')
['88', '', '89', '']
正是这里多余的空字符串导致
int()
引发异常:

>>> int('')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: ''
代码中还存在一些其他问题。仔细看看:

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
这里,
lenoffile
不是文件中的行数。它是
'Lab8.txt'
中的字符数;这两个值恰好都是
8
,但如果在该文件中添加或删除某些行,则其余代码的数字将是错误的

如果你要把这些数字和名字放在一起,然后再把计算结果写出来,你就得把这些名字放在一起

以下是解决同一任务的替代版本:

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))

另一种可能是过滤掉空字符串,方法是在
map
之前调用
filter
,或者改为使用理解(例如,
[int(numeric)表示b1中的数字。拆分(“”)表示数字]
)。在这种情况下,当一开始很容易避免空值时,这样做更有意义;在不可能的情况下,知道如何过滤是值得的。哦,我明白了。我犯了一个愚蠢的错误。如何将此文件附加到另一个输出中?
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))