Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python ValueError:以10为基数的int()的文本无效:'';怎么了?_Python_Python 3.x - Fatal编程技术网

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
loopjust
split()
相当于
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)