TheXXcoder
TheXXcoder

Reputation: 23

attempt to compare number with string (Lua)

I've been programming this

io.write("How many languages do you speak?\n")

answer = io.read()

if (answer == 1)
then
io.write("You're a monolingual beta")
elseif (answer == 2)
 then
 io.write("You're bilingual")
elseif (answer == 3)
 then
 io.write("You're a multilingual semichad")
elseif (answer == 4)
 then
 io.write("You're a polyglot")
elseif (answer == 5)
 then
 io.write("You're a super polyglot")
elseif (answer == 6)
 then
 io.write("You're an hyper polyglot")
elseif (answer == 7)
 then
 io.write("You're a ultra polyglot")
elseif (answer == 8)
 then
 io.write("You're a mega polyglot")
elseif (answer == 9)
 then
 io.write("You're an ultra-mega polyglot")
elseif (answer >= 10)
 then
 io.write("You're an infinite polyglot gigachad")
end

But when i try to execute it and i put a number, it just says the error in the title. The line of code which the error refers is this one

elseif (answer >= 10)

And i can't really understand the issue. There are no strings. Can someone please help me out?

Upvotes: 2

Views: 3465

Answers (4)

Random
Random

Reputation: 505

This is a common mistake made by many people. It's especially common if you're coming from languages with less strict typing rules. However, Lua is slightly stricter, and that's better.

The thing is that "1" and 1 are different things - The former is a string, which represents text, whereas the latter is a number, which obviously represents a number.

In fact, they're encoded differently. The string can be considered to be encoded like this:

00110001

Whereas the number can be considered to be encoded like this:

00000001

In reality, a number is a double precision floating point number, which does cool stuff with scientific notation. However, that doesn't change the fact that in binary, these are different.

The bit-level difference aside, the types are different - if the types are different, the values are not equal, like the types. And that is why you get the error: You need a number and a number.

The solution is to use tonumber, which converts a string representing an integer (decimal by default, other bases disallow integers).

Upvotes: 0

Luatic
Luatic

Reputation: 11171

To add an explanation as to how the error comes into being:

io.read() reads a string (a line). answers thus holds the age in string form.

Lua's comparisons are strict; all equality comparisons of strings against numbers fail since strings and numbers are of different types. It eventually falls through to answer >= 10 which throws the error since all ordering relations (<, >, <=, >=) only accept either two strings or two numbers (or two tables with metatables, excepting debug.setmetatable for the sake of simplicity).

answer >= 10 where answer is a string finally throws the error.

Upvotes: 2

BryceH
BryceH

Reputation: 2798

You need to convert the value of answer to a number. You can do so by using tonumber(answer).

Upvotes: 0

Paul Kulchenko
Paul Kulchenko

Reputation: 26744

answer variable contains a string as the result of io.read() call. Either add tonumber around io.read() or use io.read("n") to get a number.

Upvotes: 6

Related Questions