162 posts
Posted 06 December 2013 - 01:21 PM
Okay so basically I'm making a random program because I watched a couple ComputerCraft tutorial videos and feel the need to make cool programs because I've always thought coding to be cool, but that's besides the point. What's happening is the terminal says "Guess the number!" and it's supposed to, you guess the number and it will tell you higher/lower until you guess it right, 2 things I want to know, I know you can make it reboot at the end, I plan to say "You got it right!" sleep for about 5 seconds then reboot, I have startup set to run the program again. The other thing I want to know is how can I make it tell the difference between numbers and letters? If you put letters in then it gives an error and terminates the program, here's what I have so far.
note: If there's any spelling errors its me copying it from the server, I tested the program before writting this and it works, I just want help improving it.
term.clear()
term.setCursorPos(1,1)
print("Guess the number!(1-100)")
number = math.random(1,100)
while true do
guess = io.read()
if tonumber(guess) > number then
print("Too high!")
elseif tonumber(guess) < number then
print("Too low!")
else
print("You win!")
sleep(2)
break
end
end
Thank you to anyone who took the time to read this.
758 posts
Location
Budapest, Hungary
Posted 06 December 2013 - 03:26 PM
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
Edited on 06 December 2013 - 02:27 PM
162 posts
Posted 07 December 2013 - 06:15 AM
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 on 08 December 2013 - 05:59 AM