gpt4 book ai didi

python - 正确使用 python 的 if 语句映射函数

转载 作者:行者123 更新时间:2023-11-30 23:31:35 24 4
gpt4 key购买 nike

我尝试正确使用 map 并使用 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: invalidliteral for int() with base 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: ''

使用带有参数的str.split();然后去除多余的空格:

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

您的代码中还存在一些其他问题。仔细看看:

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

这里,lenofile不是文件中的行数。是'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))

关于python - 正确使用 python 的 if 语句映射函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19824443/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com