I have tried to use both loops as well as list comprehension. Both are unable to parse the integers even though i am trying to convert the numbers into int in a list.
我已经尝试使用这两个循环以及列表理解。即使我正在尝试将数字转换为列表中的整型,两者都无法解析整数。
student_scores = input("Input a list of student scores.")
print(student_scores)
for n in range(0, len(student_scores)):
student_scores[n] = int(student_scores[n])
print(student_scores)
student_score = [int(i) for i in student_scores]
highest_score = 0
for score in student_score:
if score > highest_score:
highest_score = score
print(f"The highest score in the class is {highest_score}")
更多回答
what is the value that you entred into this code ?
您在此代码中输入的值是什么?
It's because you are calling int()
on each character in the string, including the spaces in between the numbers.
这是因为您对字符串中的每个字符调用int(),包括数字之间的空格。
What are you typing in? Is it a list like "12 34 56"? Then you're calling int()
on each individual character, and int(' ')
is going to fail. Likewise if it's got commas. You need to use student_scores.split()
[look at documentation for arguments] to break it up into proper pieces.
你在输入什么?是不是像“123456”这样的名单?然后对每个字符调用int(),而int(‘’)将失败。同样,如果它有逗号。您需要使用STUMENT_SCORES.Split()[查看文档中的参数]将其分解成适当的片段。
优秀答案推荐
you never separate the input which is a string by default. what is your delimiter?
您永远不会分隔默认情况下为字符串的输入。您的分隔符是什么?
student_scores = input("Input a list of student scores.")
student_scores = student_scores.split(',') if ',' in student_scores else student_scores.split()
student_scores = list(map(int, student_scores))
print(student_scores)
highest_score = max(student_scores)
print(f"The highest score in the class is {highest_score}")
I added .split() at the end of the input question and was able to resolve the issue. thx for the responses
我在输入问题的末尾添加了.Split(),并且能够解决问题。回应的THX
更多回答
我是一名优秀的程序员,十分优秀!