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

[Error]

Started by Slyth, 05 May 2012 - 12:09 AM
Slyth #1
Posted 05 May 2012 - 02:09 AM
Ok, so i have created a simple program, that is kind of like a conversation.
Its pretty neat, except whenever I try to add things to it, I have problems depending on what I try.
Here is my program:

term.clear()
term.setCursorPos(1,1)
write("Whats your name?")
name=io.read()
print("Hello"..name.."how are you today?")
feeling=io.read()
if feeling=="Good" then
write("So am I!")
end


That is the part that works.Then if I try to add this, then I get "So am I!" again.

if feeling=="Bad" then
write("Oh Im sorry")
end

I am sure you probably know, but I am trying to add another thing, like if I type in "bad" after how are you today, then i get "Oh, Im sorry".
also, how do I put my code in the little white boxes to post them?
Thanks guys
Luanub #2
Posted 05 May 2012 - 02:26 AM
To use the code or spoiler tags do this without the *'s

[*code]
insert code
[*/code]

Your syntax and everything looks good. I would suggest since your only doing one input for the var that you only do one if statement as well. Try doing.. I'm also adding in some things to convert to lowercase so it wont matter if the user enters Bad BAD bad, BaD BAd, well you get the point.. Also converting the vars to local as that is much cleaner.


term.clear()
term.setCursorPos(1,1)
write("Whats your name?")
local name=io.read()
print("Hello"..name.."how are you today?")
local feeling= string.lower(io.read())
if feeling=="good" then
write("So am I!")
elseif feeling == "bad" then
write("Oh Im sorry")
end
Slyth #3
Posted 05 May 2012 - 05:04 PM
OK awesome, thank you.
Just a few more things:
what does putting "local" before the name=io.read do?
and what does this do?

local feeling= string.lower(io.read())


Thanks again!
Luanub #4
Posted 05 May 2012 - 09:32 PM
It makes the variable local to the program so that it gets dumped when the program is closed. Helps to avoid conflicts with other programs, and some other funky issues.

The string.lower converts what ever the user inputs into lower case. So if I put in my answer as Good, it would convert it to good. Just makes your data validation a little easier.
Slyth #5
Posted 06 May 2012 - 03:57 PM
ahh! i get it now! thanks. I also just created a program so that i can close the doors to my mine right from my house!
I will definitely use string.lower.