11 posts
Posted 22 December 2014 - 05:34 PM
I was trying to make a program that will tell me if a number is available, but i have one last error code …
…:18: attempt to compare nil with number
local w = 720 or 722 or 723 or 724 or 726 or 727 or 728 or 729
local k = 719
local g = 900
-- os.pullEvent = os.pullEventRaw
term.clear()
term.setCursorPos(1,1)
print("")
print("With this program can you check if a wireless number is available. Type in a number between 719 and 900. Don't use space bar !!")
print("")
write("Number: ")
while true do
x = tonumber(read())
if x > k and x < g and x ~= w then
term.clear()
term.setCursorPos(1,1)
print("")
textutils.slowPrint("This number is available! Have a nice working day!")
sleep(2)
os.reboot()
break
else
if x == w then
term.clear()
term.setCursorPos(1,1)
print("")
textutils.slowPrint("This number is is already in use, try another number!")
print("")
textutils.slowWrite("Number: ")
elseif x <= k then
term.clear()
term.setCursorPos(1,1)
print("")
textutils.slowPrint("This number is to low, it has to be a number between 719 and 900!")
print("")
textutils.slowWrite("Number: ")
elseif x >= g then
term.clear()
term.setCursorPos(1,1)
print("")
textutils.slowPrint("This number is to high, it has to be a number between 719 and 900!")
print("")
textutils.slowWrite("Number: ")
elseif type(x) == "number" then
term.clear()
term.setCursorPos(1,1)
print("")
textutils.slowPrint("This is not a number or you used space bar, try again!")
print("")
textutils.slowWrite("Number: ")
end
end
end
3057 posts
Location
United States of America
Posted 22 December 2014 - 06:00 PM
local w = 720 or 722 or 723 or 724 or 726 or 727 or 728 or 729
Is exactly the same as
local w = 720
Your problem:
x = tonumber(read())
x = nil if read() doesn't return a numerical value (ei you enter "hi" instead of '3')
11 posts
Posted 22 December 2014 - 06:51 PM
local w = 720 or 722 or 723 or 724 or 726 or 727 or 728 or 729
Is exactly the same as
local w = 720
Oh damn, i thought that if i type 720 or 723 that it will say
"This number is is already in use, try another number!" …How can i simple solve this problem ??
Your problem:
x = tonumber(read())
x = nil if read() doesn't return a numerical value (ei you enter "hi" instead of '3')
And how can i simple solve this problem ??
3057 posts
Location
United States of America
Posted 22 December 2014 - 07:32 PM
1st problem - use a table lookup.
local blacklist = {
[720] = true,
[722] = true,
--#and so on
}
Then, to see if the number is taken,
if blacklist[ num ] then
--#it's blacklisted
else
--#it's not blacklisted
end
2nd: I already provided the solution. It basically keeps calling read() until they enter a valid number.
Edited on 22 December 2014 - 06:32 PM
11 posts
Posted 22 December 2014 - 08:09 PM
1st problem - use a table lookup.
local blacklist = {
[720] = true,
[722] = true,
--#and so on
}
Then, to see if the number is taken,
if blacklist[ num ] then
--#it's blacklisted
else
--#it's not blacklisted
end
2nd: I already provided the solution. It basically keeps calling read() until they enter a valid number.
I have tried i few things but it doesn't solve anything. Can you plz give the code how you will solve this problem ??
And if i use the table lookup, it ignores this IF statement:
term.clear()
term.setCursorPos(1,1)
print("")
textutils.slowPrint("This number is is already in use, try another number!")
print("")
textutils.slowWrite("Number: ")
And when a say for example 720: "This number is available! Have a nice working day!", but the number is already in use …
1140 posts
Location
Kaunas, Lithuania
Posted 22 December 2014 - 09:15 PM
Please post your current code. Note that if you are using a lookup-table then you cannot do something like this:
if x == blacklist then
You would have to do it like this:
if blacklist[x] then
And I think here:
elseif type(x) == "number" then
you meant to check if it is
not a number like this (notice the '~=' which mean 'not equal'):
elseif type(x) ~= "number" then
And in fact, you probably might want to check if it is a number first and then try to compare it to avoid getting that 'attempt to compare nil and number' errors.
Edited on 22 December 2014 - 08:19 PM
3057 posts
Location
United States of America
Posted 22 December 2014 - 09:23 PM
I re-wrote your entire program, try this and modify from there.
local taken = {
[720] = true,
[722] = true,
[723] = true,
[724] = true,
[726] = true,
[727] = true,
[728] = true,
[729] = true,
}
local x
repeat
print( "Enter a number between 719 and 900" )
x = tonumber( read() )
until x and x >= 719 and x <= 900 and not taken[ x ]
7083 posts
Location
Tasmania (AU)
Posted 22 December 2014 - 09:33 PM
And how can i simple solve this problem ??
The idea is to check that x is a number
before you attempt to compare it against other values.
Eg, this sort of thing:
Spoiler
term.clear()
term.setCursorPos(1,2)
print("With this program can you check if a wireless number is available. Type in a number between 719 and 900. Don't use space bar !!")
print("")
write("Number: ")
local function wasteTime(text)
term.clear()
term.setCursorPos(1,2)
textutils.slowPrint(text)
print("")
textutils.slowWrite("Number: ")
end
local blacklist = {
[720] = true,
[722] = true,
--#and so on
}
local k, g = 719, 900
while true do
x = tonumber(read())
if not x then -- If x is nil (because invalid text was entered)...
wasteTime("This is not a number or you used space bar, try again!")
elseif x >= g then -- We know x can't be nil at this point.
wasteTime("This number is to high, it has to be a number between 719 and 900!")
elseif x <= k then
wasteTime("This number is to low, it has to be a number between 719 and 900!")
elseif blacklist[x] then
wasteTime("This number is is already in use, try another number!")
else
term.clear()
term.setCursorPos(1,2)
textutils.slowPrint("This number is available! Have a nice working day!")
sleep(2)
break
end
end
11 posts
Posted 23 December 2014 - 10:04 AM
Thank you all !!
But finally works this code (Bomb Bloke, your code).
term.clear()
term.setCursorPos(1,2)
print("With this program can you check if a wireless number is available. Type in a number between 719 and 900. Don't use space bar !!")
print("")
write("Number: ")
local function wasteTime(text)
term.clear()
term.setCursorPos(1,2)
textutils.slowPrint(text)
print("")
textutils.slowWrite("Number: ")
end
local blacklist = {
[720] = true,
[722] = true,
--#and so on
}
local k, g = 719, 900
while true do
x = tonumber(read())
if not x then -- If x is nil (because invalid text was entered)...
wasteTime("This is not a number or you used space bar, try again!")
elseif x >= g then -- We know x can't be nil at this point.
wasteTime("This number is to high, it has to be a number between 719 and 900!")
elseif x <= k then
wasteTime("This number is to low, it has to be a number between 719 and 900!")
elseif blacklist[x] then
wasteTime("This number is is already in use, try another number!")
else
term.clear()
term.setCursorPos(1,2)
textutils.slowPrint("This number is available! Have a nice working day!")
sleep(2)
break
end
end
<SOLVED>