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

Unexpected symbol

Started by Bennievv, 20 April 2013 - 07:33 AM
Bennievv #1
Posted 20 April 2013 - 09:33 AM
Hello,

I got some code for my new program. But (this is just a piece op the code) it says there is a unexpected symbol on line 17 (here on line 3).
It does have something to do with the "or if" part.


if read() == "upload" then
  upload()
or if read() == "download" then
  download()
or if read() == "quit" then
  print('See you soon!')
  sleep(2)
  shell.exit()

How do I fix it? Thanks!
Bennievv
Espen #2
Posted 20 April 2013 - 09:37 AM
Replace or if with elseif.

Edit:
Btw. you can edit your topic title if you click the "Use Full Editor" button when editing posts.
Edited on 20 April 2013 - 07:39 AM
Bennievv #3
Posted 20 April 2013 - 09:40 AM
Thanks! It works!
Bennievv #4
Posted 20 April 2013 - 09:53 AM
I've got another problem (sorry, I'm a programming noob):

local loginUser
  write'Username: '
   read() == local username
   if username() == false then
   print('Please enter your GitHub password.')
   monitor.setTextColor(colors.orange)
   print("Don't worry, it will only be used to authenticate you.")
   monitor.setTextColor(colors.white)
   loginPw()
  else
   monitor.setTextColor(colors.red)
	print('Please enter a password')
	monitor.setTextColor(colors.white)
	loginUser()
end
end

Here I've got some unexpected symbols too. :(/>
Espen #5
Posted 20 April 2013 - 09:55 AM
Edit:
Whoops, noticed your new post only after posting myself.
Let me put my original answer in a spoiler tag and take a look at your new problem…
SpoilerNo problem.
Also you might not want to call read() everytime, because this forces the user to enter something again and again.
I think what you really want is to have the user input something once and then check that for all kinds of conditions, kind of like this:
local input = read()
if input == "upload" then
  upload()
elseif input == "download" then
  download()
elseif input == "quit" then
  print('See you soon!')
  sleep(2)
  shell.exit()
end
Bennievv #6
Posted 20 April 2013 - 09:57 AM
No problem.
Also you might not want to call read() everytime, because this forces the user to enter something again and again.
I think what you really want is to have the user input something once and then check that for all kinds of conditions, kind of like this:
local input = read()
if input == "upload" then
  upload()
elseif input == "download" then
  download()
elseif input == "quit" then
  print('See you soon!')
  sleep(2)
  shell.exit()
end
Thanks, again! It's indeed better to do it like that.
Espen #7
Posted 20 April 2013 - 10:05 AM
Here are the things that you used wrong in general.
I pointed out only syntax errors etc., but not programming logic.

This…
local loginUser
  -- other code
end

…should be …
local function loginUser()
  -- other code
end

This…
write'Username: '
…should be …
write('Username: ')

This…
read() == local username
…should be …
local username = read()
Then again, I don't know if you really want to call a function username() after that where you check your user, or if you actually want to check the variable 'username'.
If it's the former, then the line for defining the username should not be local, as the function username() won't be able to access it, since it will be outside of its scope.

I suggest taking a look at some Lua references, there are plenty out there, for example:
http://wiki.roblox.com/index.php/Lua_Help
http://www.lua.org/pil/
Edited on 20 April 2013 - 08:06 AM
remiX #8
Posted 20 April 2013 - 10:12 AM
This…
write'Username: '
…should be …
write('Username: ')

The way he did it does work. But he can't concatenate with commas, not sure about using ..
Bennievv #9
Posted 20 April 2013 - 10:13 AM
I see…

I've edited it!
Espen #10
Posted 20 April 2013 - 10:31 AM
@remiX:
You're right, setting the parantheses for that is not necessary, it's more a habit of mine I guess.
Too often something goes awry when not using them, like e.g. with statements that check multiple conditions.
Definitely a good practice I'd encourage to adopt though. But yeah, strictly speaking it's not necessary for it to work. :)/>