简体   繁体   中英

Convert Python to Lua

I have to convert this Python program I made a while ago into a Lua program. I've done some of it, I just can't seem to format my list correctly or take the data entered by the user and pass it through my if-else statements.

The program is supposed to get the age of the user as well as the day of the week and then output their airfare. If their age is 65 or older and it's the second day of the week, the price is $75 otherwise it is $150.

Any help would be appreciated

Thanks!

Python Code

1个

Lua code

2个

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
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

check this project, i am the author, the poroject is alive so please report any i bug and i fixed as soon as possible, suggest a new a feature also i will look about it if it is possible i add it: https://github.com/MhadhbiXissam/pythonicLua

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