Python ValueError:以10为基数的int()的文本无效:'';怎么了?
我正在做一个项目,我必须创建一个均值、中位数、模式和范围计算器,但我一直无法从用户那里获取数字作为输入。代码如下:Python ValueError:以10为基数的int()的文本无效:'';怎么了?,python,python-3.x,Python,Python 3.x,我正在做一个项目,我必须创建一个均值、中位数、模式和范围计算器,但我一直无法从用户那里获取数字作为输入。代码如下: print("Mean, Median, Mode, and Range Calculator") user_input = input("Press 1 to choose Mean , 2 to choose Median, 3 to choose Mode, and 4 to choose Range") def get_num():
print("Mean, Median, Mode, and Range Calculator")
user_input = input("Press 1 to choose Mean , 2 to choose Median, 3 to choose Mode, and 4 to choose Range")
def get_num():
x = [input("Enter your numbers Without commas ie. 12 34 56: ")]
x1 = []
for i in x:
x1.append(int(i))
print(x1)
if user_input == '1':
pass
但我一直在犯这样的错误:
ValueError: invalid literal for int() with base 10: '12 34 56'
我尝试过使用映射,使用for循环浏览列表,但不起作用。我甚至不知道这个错误是什么意思。有人能解释一下吗?试试这个。
此外,用户输入
未定义。如果您使用x1
只是为了转换为int
,那么您不需要它<代码>地图为您完成此操作
def get_num():
x = map(int,input("Enter your numbers Without commas ie. 12 34 56: ").split())
return x
x1 = []
x = get_num()
for i in x:
x1.append(i)
print(x1)
不要将对
input()
的调用放入列表中。要获取输入的单词列表,请使用split()
您还可以使用列表理解
def get_num():
x = input("Enter your numbers Without commas ie. 12 34 56: ")
x1 = [x1.append(int(i)) for i in x.split()]
print(x1)
一种很好的方法是使用
映射
将输入
转换为int
,并使用列表
函数包装映射
def get_num():
x = input("Enter your numbers Without commas ie. 12 34 56: ")
x1 = list(map(lambda i: int(i), x.split()))
print(x1)
您想要x.split()这是否回答了您的问题?你不应该把
input()
放在一个列表中。是的,只要使用input()
获取数字,然后应用split()
,你就会得到一个可编辑的list
,你就可以使用for
loopjustsplit()
相当于split(“”
def get_num():
x = input("Enter your numbers Without commas ie. 12 34 56: ")
x1 = list(map(lambda i: int(i), x.split()))
print(x1)