LBPHacker, on 06 December 2013 - 03:26 PM, said:
1) Use
locals.
As your program is structured now (as seen in the OP), if the input cannot be converted to a number, the user wins. So
2) Attempt to convert the input to a number right after reading it, and tell the user they're stupid if the conversion fails.
The way I'd do all this:
term.clear()
term.setCursorPos(1,1)
print("Guess the number! (1 - 100) > ")
local number = math.random(1, 100)
while true do
local input = tonumber(read())
if not input then
repeat
print("That's not a number. > ")
input = tonumber(read())
until input
end
if input == number then
print("You won! Yay!\nReboot in ")
local cursorX, cursorY = term.getCursorPos()
local countdown = 5
for ix = countdown, 1, -1 do
term.setCursorPos(cursorX, cursorY)
term.write(tostring(ix))
sleep(1)
end
os.reboot()
end
print(string.format("That's too %s! > ", input < number and "low" or "high"))
end
As mentioned above, or at least I think I mentioned above, I'm new to coding and I don't fully understand the code, I want to use code I understand because I'm very forgetful so if I ever forget something I can go back to an old program and see how I used it so I can remember how everything works, in the end I'll get the hang of it but until then I plan to keep on learning, here's the parts of your code I don't understand
if not input then --I'm not showing full code I'm showing the parts I don't understand, just to let you know.
repeat
print("That's not a number. >") --why is that ">" there? Sorry if it's obvious, but I'm a bit "special" :)/> -- why does a "/>" show up after I put that smiley face? I keep editting the post to delete it then it comes back,
until input
local cursorX, cursorY = term.getCursorPos --I feel it's self explanatory that gets the position of the cursor, but why do you need it? I plan to clear the page and start from 1,1 so it looks better,
local countdown = 5
for ix = countdown 1, -1 do
term.write(tostring(ix))
sleep (1) --I understand this, but why?
print(string.format("that's too %s! > ", input < number and "low" or "high"))
Edited by applesauce10189, 08 December 2013 - 06:59 AM.