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

Question about User Input into print("")s.

Started by jtdavis99, 28 February 2012 - 08:57 PM
jtdavis99 #1
Posted 28 February 2012 - 09:57 PM
Is there a way to have a read() program that takes the input from the user and puts it in a later-used print("") program? So, like.
print("Hi! What is your name?")
input = read()
print("Oh, hi " input "!)

Sorry if it's a bit of an obvious question, I want to be able to respond to someone's input. Thanks!
Liraal #2
Posted 28 February 2012 - 10:13 PM
print("Oh, hi "..input.."!)
jtdavis99 #3
Posted 28 February 2012 - 10:17 PM
print("Oh, hi "..input.."!)

Ah, thanks so much. I knew it was something like python :D/>/>
Luanub #4
Posted 28 February 2012 - 10:19 PM
I would also do


local input = read()

avoid global vars if you can.
Chub1337 #5
Posted 28 February 2012 - 10:26 PM
I would also do


local input = read()

avoid global vars if you can.

Why's that? I''ve so far happily used global vars? Actually, I almost never use 'local'

-Chub1337
Luanub #6
Posted 28 February 2012 - 10:52 PM
They are bad for a couple of reasons. One they are global, so they are everywhere. Before I knew better I had problems with scripts conflicts due to using global vars.

Global variables are also persistant and may be kept after the program is closed. Meaning that if someone entered "hello" for the input, and you restart the program and simply put print (input), you could get "hello" as the output.

local = much cleaner and safer to use.
Espen #7
Posted 28 February 2012 - 10:54 PM
I would also do


local input = read()

avoid global vars if you can.

Why's that? I''ve so far happily used global vars? Actually, I almost never use 'local'

-Chub1337
There are good reasons to declare your variables local whenever you can.
A quote from the PIL about local vs. global variables:
It is good programming style to use local variables whenever possible. Local variables help you avoid cluttering the global environment with unnecessary names. Moreover, the access to local variables is faster than to global ones.
Source: http://www.lua.org/pil/4.2.html

EDIT: Ninja'd by luanub. :D/>/>
Edited on 28 February 2012 - 09:54 PM
Chub1337 #8
Posted 28 February 2012 - 10:59 PM
Hmm, alright, from now it it'll be local variables! :D/>/>

Just one question though, if I define a local variable at the top of a program, is it still useable troughout the entire program?

-Chub1337
Espen #9
Posted 28 February 2012 - 11:27 PM
Hmm, alright, from now it it'll be local variables! :)/>/> Just one question though, if I define a local variable at the top of a program, is it still useable troughout the entire program? -Chub1337
Local variables can be accessed within the block they were declared, including nested functions within that block.
So to answer your question: Yes. :)/>/>
But beware that you can't access local variables (or functions) of one program from within another program.
Because from the point of view of the local variables the external program is outside of their scope.

As a mnemonic: you can use local variables from the outside-in, but not from the inside-out. ^_^/>/>
Chub1337 #10
Posted 29 February 2012 - 09:09 AM
Alright! Thanks for the help and a good day to you, fine sir. :o/>/>

-Chub1337