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

Could This Work?

Started by overfritz, 09 October 2013 - 09:02 PM
overfritz #1
Posted 09 October 2013 - 11:02 PM
Considering I'm returning to Lua, I'm not 100% sure if this code will work. I feel like it might, but also that it might not. It's an alternative to dj, but I want it to be able to check for audio, say what song it is, play it, and stop it (with the option to eject it). It at first assumes the drive is on the bottom, but it can be changed by altering the variable "drive" (at least, how I want it to be).

http://pastebin.com/jjX9BKBx - Jukebox Code
LBPHacker #2
Posted 10 October 2013 - 12:24 AM
  • input is never declared - how should Lua know what it is?
  • You're messing with the disk API, so you don't have to wrap the drive itself,
  • Missing parentheses at line 15.
  • In an if statement, something = true is not a valid expression. Use the == operator for that purpose. And you don't have to use == true anyways.
  • Again, invalid expression: input == "yes" or "no". I have no idea why people think that would work. (Is there a language in which that expression is valid? I'm asking, seriously. If there is one, let me know.) Should be input == "yes" or input == "no".
  • Use locals.
I spotted those at a first glance. There might be more.

EDIT: There is an awesome thing called debugging. It's about observing the program, watching the errors it throws, and fixing bugs. It's not that complicated, try it for yourself:
  1. Write some code.
  2. Run the program.
  3. If there are bugs or the program doesn't work the way you want it to, go back to step 1.
  4. Profit.
Man, read the official Programming in Lua.
theoriginalbit #3
Posted 10 October 2013 - 12:55 AM
Again, invalid expression: input == "yes" or "no". I have no idea why people think that would work. (Is there a language in which that expression is valid? I'm asking, seriously. If there is one, let me know.)
Python(ish)
Spoiler

if input == "yes" == "no" == "maybe":
  print("Thank you")
elif input != "":
  print("Invalid response");
else:
  print("Did not supply response");
A time I used it was in some checking logic for wins in TicTacToe game for uni

for row in WIN_SET:
  if board[row[0]] == board[row[1]] == board[row[2]] != ' ':
	return board[row[0]]; # return the winner
as opposed to

for row in WIN_SET:
  if board[row[0]] == board[row[1]] and board[row[0]] == board[row[2]] and board[row[0]] != ' ':
	return board[row[0]]; # return the winner

EDIT: Oh and I do agree btw with LBPHacker, learn to debug programs, it will save you heaps of time, and give you a sense of accomplishment. We're not your slaves, we're not here to test or fix problems in your code. We're here to give you advise when you've run into problems that you've spent a good amount of time on fixing yourself and you just cannot find the problem.
Edited on 09 October 2013 - 11:00 PM