【问题标题】:Convert Table Elements to Integers将表格元素转换为整数
【发布时间】:2018-08-20 18:15:03
【问题描述】:

我正在尝试创建一个整数列表,类似于人们所说的python

x = input("Enter String").split() # 1 2 3 5
x = list(map(int,x)) # Converts x = "1","2",3","5" to x = 1,2,3,5

这是我的代码要求输入,然后将输入拆分为一个表,我需要帮助将表的内容转换为整数,因为它们稍后在函数中被引用,我得到一个字符串与整数比较错误。我尝试更改拆分 for 循环以获取数字,但这不起作用,我熟悉 python 转换但不熟悉 Lua,所以我正在寻找一些指导来转换我的表格或更好地处理这个问题。

 function main()
  print("Hello Welcome the to Change Maker - LUA Edition")
  print("Enter a series of change denominations, separated by spaces")
  input = io.read()
  deno = {}
  for word in input:gmatch("%w+") do table.insert(deno,word) end
end
--Would This Work?:
--for num in input:gmatch("%d+") do table.insert(deno,num) end

【问题讨论】:

标签: lua


【解决方案1】:

只需使用 tonumber 将您的数字字符串转换为数字

local number = tonumber("1")

所以

 for num in input:gmatch("%d+") do table.insert(deno,tonumber(num)) end

应该做的伎俩

【讨论】:

    猜你喜欢
    • 2014-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多