Help with Lua loop + counter

coded in Lua

my code is not functioning the way it suppose to and it was working fine a day ago but all the sudden it just changes with no explanation

UPDATE code has been cleaned but now just trying to loop it

https://replit.com/@DanielHernan163/In-Class-Psuedocode-to-Lua#GuessingGame.lua

--Computer picks a number from 1 to 10.
--User tries to guess the computer number.
--User is allowed a certain number of attempts (7)
--If user guesses correctly, user wins.
--If user does not guess correctly within the allowed number of attempts, user loses.
--Print a winning or losing message after each game.
--Allow the user to replay as many times as they want.]]

math.randomseed(os.time())
math.random() ; math.random() ; math.random() 

checknum = math.random(1,10)
print (checknum)
done = false
counter = 0
num1 = nil
userChoice = nil

while done == false do
  while counter < 7 do
  while num1 == nil or not tonumber(num1) or num1 == "" do
   print("please give me a guess: ")
   num1 = io.read()


   if counter == 7 then
     print ("ran out of tries")
     break
     end

 -- else if num1 ~= checknum then
  -- print (" ")
  --    print("Try Again: ")
  -- counter = counter + 1
  -- num1 = nil

    --else if num1 == checknum then
  -- print ("congrats its: ".. checknum)
     -- end

   if tonumber(num1) == tonumber(checknum) then
    print ("congrats its: ".. checknum)
    print ("Y to stop, other key to continue")
    userChoice = io.read()
      
     if string.upper (userChoice) == "Y" then
     done = true
     break
     end
     userChoice = nil

     -- if num1 == nil or not tonumber(num1) or num1 == "" then
        --  counter = 0
        --  checknum = math.random(1,10)
        --  print("please give me a guess: ")
       --   num1 = io.read()


   else print("Try Again: ")
   counter = counter + 1
   num1 = nil



    end
   end
  end
end

Hi @DanielHernan163 can you please post a link to your repl? This way others in the community can suggest some ideas.

1 Like

I’m not sure if it’ll cause an error, butnum1 should probably be defined before the loop, probably right when counter is, even if it’s justnum1 = nil.

What are you expecting to happen and what’s actually happening (that isn’t what you expect)?

sorry never done lua before ive done C++ and java but what another issue is that it stopping after 5 try against but its set up to 7 it was working when I worked on the code 2 days ago and now it just changed

1 Like

link has been added…

1 Like

You’ve got an if statement and then a separate if chain. In both you increment counter. Did you intend too increment counter after printing try again.

so counter is set to 0, if the answer is wrong it goes up by 1, once counter gets to 7 the game stops I don’t understand why it stops at 5, it should only go up when the answer is wrong

1 Like

I think that inside loop stuff are ok, maybe just fix the outside loop? :thinking:

1 Like

ive fixed most of it now i just need to repeat it now

2 Likes