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

attempt to call nil

Started by bigwillanderson, 02 November 2012 - 06:47 AM
bigwillanderson #1
Posted 02 November 2012 - 07:47 AM
Hello, my program is an all purpose OS that currently contains a calculator, a timer and a guess the number game. I tried running it, but after typing "go" to get in the program, it said "attempt to call nil" on line 173. Here is the code:


function guess()
do
numb = math.random(1,100)
tries = 1
triesmax = 7
version = 1.1
win = false
end

do
term.clear()
textutils.slowPrint("———————————–")
textutils.slowPrint("| Geuss the Number |")
textutils.slowPrint("———————————–")
print("Enter to continue")
io.read()
print("You have 6 guesses. Guess the number before you run out of guesses")
end

while true do
if tries > triesmax then
break
else
print("Guess. [1 - 100, whole number]")
guess = io.read()
guess = tonumber(guess)
if guess == numb then
win = true
break
else
print("Incorrect")
if guess > numb then
print("Your guess was too high")
elseif guess < numb then
print("Your guess was too low")
else
print("This is a bug. Inform the WILL OS lab")
end
tries = tries + 1
end
end
end

do
term.clear()
textutils.slowPrint("GAME OVER")
if win == true then
print("YOU WIN!")
else
print("YOU LOSE")
end
end


function options1()
input = read()

if input == "t" then
term.clear()
term.setCursorPos(1,1)
timer()
end

if input == "c" then
term.clear()
term.setCursorPos(1,1)
calculator()
end

if input == "?" then
term.clear()
term.setCursorPos(1,1)
Guess()
end
end

function home()
print "—————————————–"
print "|,.,.,.,.,.,.,.,.WILL OS ,.,.,.,.,.,.,.,|"
print "|[T] Timer | |"
print "|[C] Caculator | |"
print "|[?] Geuss the number| |"
print "—————————————–"
options1()
end

function title()
term.clear()
term.setCursorPos(1,1)
home()
end

function timer()
minutes = 0
hours = 0
function clock()
for seconds = 0, 60, 1 do
shell.run("clear")
print "WILL OS (copyright 2012)"
print "———————————"
print (hours, ":",minutes,":", seconds)
print "———————————"
print "ATTENTION: You must still use CTRL-T"
print "to exit this program"
print "A fix is on it's way!"
if seconds == (60) then
minutes = minutes + 1
clock()
end
if minutes == (60) then
hours = hours + 1
minutes = 0
end
sleep(1)
end
seconds = 0
end
clock()
end
end

function calculator()
print "WILL OS (copyright 2012)"
print "Do you want to add, subtract, multiply, or divide?"
op = read()
print ""
print "Enter first number"
num1 = tonumber(read())
print ""
print "Enter second number"
num2 = tonumber(read())
print ""

if op == "add" then
result = num1+num2
print(result)
sleep(4)
title()
end

if op == "multiply" then
result = num1*num2
print(result)
sleep(4)
title()
end

if op == "divide" then
result = num1/num2
print(result)
sleep(4)
title()
end

if op == "subtract" then
result = num1-num2
print(result)
sleep(4)
title()
end
end

print "——————————————-"
print "| Welcome to WILL OS! |"
print "——————————————-"
print "Current build - WILLOSDEVBUILD2"
print "Current Version - WILL OS ALPHA 2"

print "[go] Get Started!"
print "[info] Information. Read before use"

if input == "go" then
title()
end

if input == "info" then
info()
end
casr144 #2
Posted 02 November 2012 - 08:02 AM
What is on line 173? (As much as i would love to count the lines I can't really be bothered)
bigwillanderson #3
Posted 02 November 2012 - 08:08 AM
line 173 is title()

under the if input == "go" bit
casr144 #4
Posted 02 November 2012 - 08:22 AM
if input == "go" bit then?

nvm lol
casr144 #5
Posted 02 November 2012 - 08:26 AM
try replacing that part with:
if input == "go" then
title()
elseif input == "info" then
info()
end
bigwillanderson #6
Posted 02 November 2012 - 08:30 AM
ok thanks ill try it
bigwillanderson #7
Posted 02 November 2012 - 08:37 AM
it's still doing it, but as soon as i boot the program up. I cant even input "go".
casr144 #8
Posted 02 November 2012 - 09:02 AM
thats really weird.
casr144 #9
Posted 02 November 2012 - 09:05 AM
are you sure that you have declared the variable 'input'?
put 'input = read()' before the 'if input == "go" bit
casr144 #10
Posted 02 November 2012 - 09:07 AM
then it should work
casr144 #11
Posted 02 November 2012 - 09:48 AM
Success?
bigwillanderson #12
Posted 02 November 2012 - 10:00 AM
hang on, ill try it
bigwillanderson #13
Posted 02 November 2012 - 10:05 AM
it's still giving the "attempt to call nil" message, but the first screen is working

this is wierd
sjele #14
Posted 02 November 2012 - 10:17 AM
Success?

Quad post! instead of quad post use the old edit button.

To op. Code tags ( [*code] –code [*/code] ) REmove the *s
Ask a pro section much better, ans faster, and the correct place to post this
Spoilers for long code. [*spoiler] –code [/*spoiler] remove 's
Laywan #15
Posted 02 November 2012 - 10:27 AM
Try this one, it seems to work


Spoiler
function guess()
  numb = math.random(1,100)
  tries = 1
  triesmax = 7
  version = 1.1
  win = false

  term.clear()
  textutils.slowPrint("-----------------------------------")
  textutils.slowPrint("| Guess the Number |")
  textutils.slowPrint("-----------------------------------")
  print("Enter to continue")
  io.read()
  print("You have 6 guesses. Guess the number before you run out of guesses")

  while true do
	if tries > triesmax then
	  break
	else
	  print("Guess. [1 - 100, whole number]")
	  guess = io.read()
	  guess = tonumber(guess)
	  if guess == numb then
		win = true
		break
	  else
		print("Incorrect")
		if guess > numb then
		  print("Your guess was too high")
		elseif guess < numb then
		  print("Your guess was too low")
		else
		  print("This is a bug. Inform the WILL OS lab")
		end
		  tries = tries + 1
		end
	  end
  end

  term.clear()
  textutils.slowPrint("GAME OVER")
  if win == true then
	print("YOU WIN!")
  else
	print("YOU LOSE")
  end
end

function timer()
  minutes = 0
  hours = 0

  function clock()
	for seconds = 0, 60, 1 do
	  shell.run("clear")
	  print "WILL OS (copyright 2012)"
	  print "---------------------------------"
	  print (hours, ":",minutes,":", seconds)
	  print "---------------------------------"
	  print "ATTENTION: You must still use CTRL-T"
	  print "to exit this program"
	  print "A fix is on it's way!"
	  if seconds == (60) then
		minutes = minutes + 1
		clock()
	  end
	  if minutes == (60) then
		hours = hours + 1
		minutes = 0
	  end
	  sleep(1)
	end
	seconds = 0
  end
  clock()
end

function calculator()
  print "WILL OS (copyright 2012)"
  print "Do you want to add, subtract, multiply, or divide?"
  op = read()
  print ""
  print "Enter first number"
  num1 = tonumber(read())
  print ""
  print "Enter second number"
  num2 = tonumber(read())
  print ""

  if op == "add" then
	result = num1+num2
	print(result)
	sleep(4)
	title()
  end

  if op == "multiply" then
	result = num1*num2
	print(result)
	sleep(4)
	title()
  end

  if op == "divide" then
	result = num1/num2
	print(result)
	sleep(4)
	title()
  end

  if op == "subtract" then
	result = num1-num2
	print(result)
	sleep(4)
	title()
  end
end

function options1()
  input = read()

  if input == "t" then
	term.clear()
	term.setCursorPos(1,1)
	timer()
  end

  if input == "c" then
	term.clear()
	term.setCursorPos(1,1)
	calculator()
  end

  if input == "?" then
	term.clear()
	term.setCursorPos(1,1)
	guess()
  end
end

function home()
  print "-----------------------------------------"
  print "|,.,.,.,.,.,.,.,.WILL OS ,.,.,.,.,.,.,.,|"
  print "|[T] Timer | |"
  print "|[C] Caculator | |"
  print "|[?] Geuss the number| |"
  print "-----------------------------------------"
  options1()
end

function title()
  term.clear()
  term.setCursorPos(1,1)
  home()
end

print "-------------------------------------------"
print "| Welcome to WILL OS! |"
print "-------------------------------------------"
print "Current build - WILLOSDEVBUILD2"
print "Current Version - WILL OS ALPHA 2"
print "[go] Get Started!"
print "[info] Information. Read before use"
input = read()
if input == "go" then
  title()
elseif input == "info" then
  info()
end 
remiX #16
Posted 02 November 2012 - 11:25 AM
Where have you defined a function for info() :?


Btw this should be in Ask a Pro.
casr144 #17
Posted 02 November 2012 - 12:16 PM
lol soz sjele
Cruor #18
Posted 02 November 2012 - 07:55 PM
Do not ask questions about code in programs section, moved to ask a pro.
bigwillanderson #19
Posted 03 November 2012 - 02:29 AM
ok, sorry, i'm pretty new here
bigwillanderson #20
Posted 03 November 2012 - 02:30 AM
I haven't defined the info() function yet, but that doesn't appear to have made a difference
bigwillanderson #21
Posted 03 November 2012 - 02:54 AM
Thanks Laywan, It worked!