Nil

Hi!
I’m trying to learn how to code in codea. I’m reading an e-book named “Lua for beginners” and in chapter 10 it’s a code which is this:

while wins.a<n and wins.b<n do str=""
--toss coins until someone wins
--check this by looking for a match
while string.find(str,a)==nil and string.find(str,b)==nil do
--add a random letter
--math.random gives a random number 0-1 unless you specify a range if math.random()<.5 then str=str.."H" else str=str.."T" end
end
--see who won
if string.find(str,a)~=nil then wins.a=wins.a+1 else wins.b=wins.b+1 end
end
--return the number of wins for each player return wins.a,wins.b
end --end of function

When I’m writing “nil” in codea it will only make error. What can I use instead of nil?

@Pontus712 when you post code in the forums, put three tilde characters ~~~ at the top and bottom of the code, then it will display with proper line-endings and indentation.

@Pontus712 I added the 3 ~'s to your code above for you. Here is the complete code for the example you posted above. There was nothing wrong with the code, but it was hard copying the code and getting it to run. Some of the actual code ended up in the comments.


function setup() 
    myPattern = "TTH"
    --Codea takes the opposite of your first letter 
    --plus the first two letters of your choice
    if string.sub(myPattern,1,1)=="H" then
        Codea="T"..string.sub(myPattern,2,3) 
    else
        Codea="H"..string.sub(myPattern,1,2) 
    end
    print("You chose",myPattern) 
    print("I chose",Codea)    
    you,me= PlayGame(myPattern,Codea,100)
    print ("You won",you,"games")
    print("I won",me,"games")
    if you>me then 
        print("You won!") 
    else 
        print("I won!") 
    end 
end   
    
function PlayGame(a,b,n)
    --initialise number of wins
    --the a and b in here are not the same as the a and b passed in above local 
    wins={a=0,b=0}
    --keep going until one of them wins
    while wins.a<n and wins.b<n do 
        str=""
        --toss coins until someone wins
        --check this by looking for a match
        while string.find(str,a)==nil and string.find(str,b)==nil do
            --add a random letter
            --math.random gives a random number 0-1 unless you specify a range 
            if math.random()<.5 then 
                str=str.."H" 
            else 
                str=str.."T" 
            end
        end
        --see who won
        if string.find(str,a)~=nil then 
            wins.a=wins.a+1 
        else 
            wins.b=wins.b+1 
        end
    end
    --return the number of wins for each player 
    return wins.a,wins.b
end

OK, thank you guys! :slight_smile: @yojimbo2000 @dave1707

@dave1707 I have just started to lear how to programming and I wonder what’s mean with the numbers in all of the “string.sub(myPattern,?,?)”

@Pontus712, have a look at the documentation for questions like that. string.sub(pattern, start, end means get the chunk from the string between start and end

@Pontus712 - try googling string.sub Lua (Lua is the language behind Codea).

Okey, now I understand what’s mean with that