Reputation:
when i type something else than + - * or / it still prints everything out only in the end it says "invalid"
print("+-*/?")
method = io.read()
if method == "+" or "-" or "*" or "/" then
print("type a number")
num1 = io.read()
print("type another number")
num2 = io.read()
elseif method ~= "+" or "-" or "*" or "/" then
print("invalid")
end
if method == "+" then
plusnum = num1 + num2
print(plusnum)
elseif method == "-" then
minusnum = num1 - num2
print(minusnum)
elseif method == "*" then
timesnum = num1 * num2
print(timesnum)
elseif method == "/" then
percentnum = num1 / num2
print(percentnum)
end
Upvotes: 1
Views: 82
Reputation: 28950
Your mistake is in the following line:
if method == "+" or "-" or "*" or "/" then
From Lua 5.3 Reference Manual 3.4.5 Logical Operators
The logical operators in Lua are and, or, and not. Like the control structures (see §3.3.4), all logical operators consider both false and nil as false and anything else as true.
So no matter what method is, the condition of your if statement will always evaluate to true
as you're or
ing at least one true
value which results in true
.
As Egor already mentioned you have to use:
if method == "+" or method == "-" or method == "*" or method == "/" then
Upvotes: 1