Home > Back-end >  Convert Python to Lua
Convert Python to Lua

Time:06-16

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

CodePudding user response:

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