首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将Python转换为Lua

将Python转换为Lua
EN

Stack Overflow用户
提问于 2022-06-15 22:34:33
回答 1查看 628关注 0票数 -3

我必须把我刚才做的这个Python程序转换成一个Lua程序。我已经做了一些,我只是不能正确地格式化我的列表,或者把用户输入的数据传递给我的if-else语句。

该程序应该得到用户的年龄以及一周中的一天,然后输出他们的机票。如果他们的年龄在65岁或以上,这是一周的第二天,价格是75美元,否则是150美元。

如能提供任何帮助,将不胜感激。

谢谢!

Python代码

Lua码

代码语言:javascript
运行
复制
week_day = {'Monday = 1', 'Tuesday = 2', 'Wednesday = 3', 'Thursday = 4','Friday = 5', 'Saturday = 6', 'Sunday =  7'} 

print("Enter your age:")
local ans = io.read()

print("Enter the day of the week (1-7):")
local wkd = io.read()

if (ans >= 65) and (wkd == 2) then
  print("Your fair is $75", "\n")

    else
  print("Your fair is $150", "\n")

    end
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-06-15 22:57:40

代码语言:javascript
运行
复制
io.write("Enter your age: ") -- use io.write, to print text, without a newline, so you can type on the same line
local ans = tonumber(io.read()) -- io.read returns a string, you need to convert it with tonumber

io.write("Enter the day of the week (1-7): ")
local wkd = tonumber(io.read())

if (ans >= 65) and (wkd == 2) then
    print("Your fair is $75", "\n") -- \n is not really nessesary as print creates a new line after
else
    print("Your fair is $150", "\n")
end
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72638567

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档