这是程序
assignments = (input(“assignments:”).split(",") #enter number separeted by comma
print(type(assignments[0]))
输出为类str
如何将数字保存为整数而不是字符串,
我已经尝试过
assignments = int(input("assignments:")).split(",")
但它给了我错误
值错误:int(( 的文字无效,底数为 10:#numbers 进入
您不能将整个list
(或包含所有逗号的输入字符串(作为一个整体int
,您必须单独对拆分令牌执行此操作,例如使用列表推导:
assignments = [int(x) for x in input(“assignments:”).split(",")]
或使用map
:
assignments = list(map(int, input(“assignments:”).split(",")))