-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculator.lua
More file actions
33 lines (30 loc) · 1012 Bytes
/
Copy pathcalculator.lua
File metadata and controls
33 lines (30 loc) · 1012 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
function Calculator() -- doesn't need a function
print("***************************")
print("A SIMPLE CALCULATOR IN LUA")
print("ENTER INPUTS WITHOUT SPACES")
print("***************************\n\n")
while true do
io.write("Enter an operation (+, -, *, /) | 'q' to quit: ")
local option = io.read()
if option == 'q' or option == 'Q' then
break
end
io.write("Enter number 1 (double): ")
local num_1 = tonumber(io.read())
io.write("Enter number 2 (double): ")
local num_2 = tonumber(io.read())
local total
-- Lua has no switch cases and I don't feel like implementing my own
if option == '+' then
total = num_1 + num_2
elseif option == '-' then
total = num_1 - num_2
elseif option == '*' then
total = num_1 * num_2
else
total = num_1 / num_2 -- Default is division | could've just dont if instad of else-if statements
end
print("The result is: " .. total)
end
end
Calculator() -- call Calculator function