简体   繁体   中英

Here's my code. I am trying to get user input of integers, go through a function utilizing map and returning a list of cubed results

I am using Python 3

vals = int(input("Enter comma separated numbers"))

def cube(nums):
    return nums**3

print(list(map(cube,vals)))

I get this error when I input 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'

This works when I put the integers myself, though:

vals = [2,3]
def cube(nums):
    return nums**3

alist = (list(map(cube,vals)))

您应先将用户的输入转换为整数序列(或浮点数,如果需要的话),然后才能映射vals以执行数字运算:

vals = map(int, input("Enter comma separated numbers").split(','))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM