gpt4 book ai didi

Python List -- ValueError: invalid literal for int() with base 10: ' ' [duplicate](Python List--ValueError:基数为10的int()的文本无效:‘’[Duplate])

转载 作者:bug小助手 更新时间:2023-10-25 11:35:31 31 4
gpt4 key购买 nike




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


更多回答

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