This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
RazorFlint's profile picture

Code Problem?

Started by RazorFlint, 25 February 2012 - 06:26 PM
RazorFlint #1
Posted 25 February 2012 - 07:26 PM
So here's my code:

term.clear()
term.setCursorPos(1,1)
random = math.random(1,10)
textutils.slowPrint("Guess The Number Game!")
print("Random Number = "..random)
sleep(2)
term.clear()
term.setCursorPos(1,1)
textutils.slowWrite("Guess A Number Between 1 And 10: ")
number = read()
sleep(2)
term.clear()
term.setCursorPos(1,1)

if number == random then
textutils.slowPrint("Well Done! Have A Cookie (:")
redstone.setOutput("back",true)
sleep(1)
term.clear()
term.setCursorPos(1,1)
redstone.setOutput("back", false)
textutils.slowPrint("Enjoy! :)/>/>")
sleep(2)
term.clear()
term.setCursorPos(1,1)
textutils.slowPrint("Shutting Down...")
sleep(2)
os.shutdown()

else
textutils.slowPrint("Incorrect :(/>/>")
sleep(2)
term.clear()
term.setCursorPos(1,1)
textutils.slowPrint("Shutting Down...")
sleep(2)
os.shutdown()
end

And every time you get it right or wrong it always say Incorrect.
I even made it output the number and it's still not right.
Suggestions?
Liraal #2
Posted 25 February 2012 - 07:45 PM
your 'number' is actually a string. try

number = tonumber(read())
Casper7526 #3
Posted 25 February 2012 - 07:46 PM
number = read()

when you read input from a user, that input is always = to a string
meaning number = "5" not number = 5
What you need to do is convert the string to a number


number = read()
number = tonumber(number)
RazorFlint #4
Posted 25 February 2012 - 08:39 PM
Thanks guys!