【python】ValueError: could not convert string to float:
发布网友
发布时间:2022-04-25 07:08
我来回答
共1个回答
热心网友
时间:2023-11-05 21:31
# -*- coding: utf-8 -*-
i = 60
while i<=100:
gpa = '%.2f' % (4-(100-i)*(100-i)*3/1600)
a = '%3d' % i
print (a,' ',gpa)
i = i+1
print ('请输入您各门课的分数(或者绩点)及学分\n\n以回车结束一门课的输入,再一次回车结束所有输入')
score = open('score.txt', 'w')
line = '1'
while line != '':
line = input()
score.writelines(line)
#score.writelines('\n')把这行注释掉,回车不能转为float报的错
score.close()
score = open('score.txt', 'r')
AllLines = score.readlines()
gpa = 0.0
g = 0.0
for EachLine in AllLines:
q = EachLine.find(' ')
l = len(EachLine)
m = EachLine[0:q]
s = EachLine[(q+1):l]
m = float(m)
s = float(s)
if m >= 60:
m = (4-(100-m)*(100-m)*3/1600)
elif m >= 5:
m = 0
m = m*s
gpa = gpa + m
g = g+s
gpa = gpa/g
print (gpa)
追问y去掉回车后面的代码要好好改改了……下课回去试试
追答..................................
热心网友
时间:2023-11-05 21:31
# -*- coding: utf-8 -*-
i = 60
while i<=100:
gpa = '%.2f' % (4-(100-i)*(100-i)*3/1600)
a = '%3d' % i
print (a,' ',gpa)
i = i+1
print ('请输入您各门课的分数(或者绩点)及学分\n\n以回车结束一门课的输入,再一次回车结束所有输入')
score = open('score.txt', 'w')
line = '1'
while line != '':
line = input()
score.writelines(line)
#score.writelines('\n')把这行注释掉,回车不能转为float报的错
score.close()
score = open('score.txt', 'r')
AllLines = score.readlines()
gpa = 0.0
g = 0.0
for EachLine in AllLines:
q = EachLine.find(' ')
l = len(EachLine)
m = EachLine[0:q]
s = EachLine[(q+1):l]
m = float(m)
s = float(s)
if m >= 60:
m = (4-(100-m)*(100-m)*3/1600)
elif m >= 5:
m = 0
m = m*s
gpa = gpa + m
g = g+s
gpa = gpa/g
print (gpa)
追问y去掉回车后面的代码要好好改改了……下课回去试试
追答..................................
热心网友
时间:2023-11-05 21:31
# -*- coding: utf-8 -*-
i = 60
while i<=100:
gpa = '%.2f' % (4-(100-i)*(100-i)*3/1600)
a = '%3d' % i
print (a,' ',gpa)
i = i+1
print ('请输入您各门课的分数(或者绩点)及学分\n\n以回车结束一门课的输入,再一次回车结束所有输入')
score = open('score.txt', 'w')
line = '1'
while line != '':
line = input()
score.writelines(line)
#score.writelines('\n')把这行注释掉,回车不能转为float报的错
score.close()
score = open('score.txt', 'r')
AllLines = score.readlines()
gpa = 0.0
g = 0.0
for EachLine in AllLines:
q = EachLine.find(' ')
l = len(EachLine)
m = EachLine[0:q]
s = EachLine[(q+1):l]
m = float(m)
s = float(s)
if m >= 60:
m = (4-(100-m)*(100-m)*3/1600)
elif m >= 5:
m = 0
m = m*s
gpa = gpa + m
g = g+s
gpa = gpa/g
print (gpa)
追问y去掉回车后面的代码要好好改改了……下课回去试试
追答..................................