我正在使用Python 3
vals = int(input("Enter comma separated numbers"))
def cube(nums):
return nums**3
print(list(map(cube,vals)))
输入 2,3 时出现此错误
#Traceback (most recent call last):
File "/Users/administrator/Documents/Python/Master Python/Day7a.py", line 15, in <module>
vals = int(input("Enter comma separated numbers"))
ValueError: invalid literal for int() with base 10: '2,3'
不过,当我自己输入整数时,这有效:
vals = [2,3]
def cube(nums):
return nums**3
alist = (list(map(cube,vals)))
您应该将用户的输入转换为整数序列(或浮点数,如果需要(,然后才能映射vals
以执行数值运算:
vals = map(int, input("Enter comma separated numbers").split(','))