This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.
  • Index
  •  » Miscellaneous
  •  » Does anyone (mostly ihaveamac) know why this Lua program won't work?

#1 2011-04-15 17:32:51

ThePCKid
Scratcher
Registered: 2009-09-16
Posts: 1000+

Does anyone (mostly ihaveamac) know why this Lua program won't work?

Code:

print(" /\\  /\\ ath")
print("/  \\/  \\ adness")
print("How high should the numbers be? (not the answer): ")
n = io.read("*n")

if (n == nil) then
    print("No number found. Choosing 10.")
    n = 10
end

function prob()
    math.randomseed(tonumber(tostring(os.time()):reverse():sub(1,6))) -- Better random numbers
    print("\n")
    x = math.random(4)
    if (x == 1) then
        x = "+"
    elseif (x == 2) then
        x = "-"
    elseif (x == 3) then
        x = "*"
    elseif (x == 4) then
        x = "/"
    end
    x = tostring(math.random(n) .. x .. math.random(n))
    print(x)
    y = io.read("*n")
    x = loadstring(x)()
    if (y == x) then
        print(y .. " is correct.")
    else
        print(y .. " is incorrect.")
    end
    prob()
end

prob()

After I try to type in an answer to the first problem, the program throws an error. This is the output:

Code:

...\Desktop\Hunter - Other\Lua stuff\test.lua:27: attempt to call a nil value
stack traceback:
        ...\Desktop\Hunter - Other\Lua stuff\test.lua:27: in function 'prob'
        ...\Desktop\Hunter - Other\Lua stuff\test.lua:36: in main chunk
        [C]: in function 'dofile'
        stdin:1: in main chunk
        [C]: ?

This is stdin:

Code:

dofile("C:\\Users\\Owner\\Desktop\\Hunter - Other\\Lua stuff\\test.lua")

Does anyone know how I can fix this?

Offline

 
  • Index
  •  » Miscellaneous
  •  » Does anyone (mostly ihaveamac) know why this Lua program won't work?

Board footer