pyhton中出现无法将字符串转换为浮点错误



这是代码

#l is a list that stores all the GPAs of 10 students
l = []
#iterates the loop to collect GPAs of 10 members in the class
for i in range(1, 11):
j = int(float(input()))  #this is where the error is
l.append(j)
#find the average GPA of the class and store the average in the "result" variable
result = sum(l)/10
#printing the result
print("Average is: ",result)

一开始是j=int(input(,这也给出了一个错误。我是python 的新手

我运行了您的代码,它工作起来没有任何问题!您必须在终端中给出每个值,然后按enter键。

用于输入验证。从tryexcept块开始。

for i in range(0, 3):
n = input('Enter a value..n')
while type(n) is not float:
try:
float(n)
break
except ValueError:
n = input('Try again..n')
#append

为什么需要将该输入转换为float?你所需要的只是检查输入,可以用try-and-expect转换为int,然后当你每次得到浮点数都除以一个数字时,我运行你的代码,它可以工作吗?你可以编辑你的问题并将错误放在上吗

score_list =[]
for num in range(10):
number = int(float(input("enter a number")))
score_list.append(number)

results = sum(score_list)/len(score_list)
print(results)

最新更新