Omnimaga

General Discussion => Technology and Development => Computer Programming => Topic started by: Munchor on May 31, 2011, 02:33:35 pm

Title: Lua Loop Defined By User
Post by: Munchor on May 31, 2011, 02:33:35 pm
I'd like to know how to get input from the user (a number) and then looping the number the user entered.

So it'd work like this

Code: [Select]
Number: 2
Running through loop : 0
Running through loop : 1

Now, i can print and read from user:

Code: [Select]
print ("Enter number: ")
max_value = io.read()

My problem is looping max_value number of times, thanks.
Title: Re: Lua Loop Defined By User
Post by: Ashbad on May 31, 2011, 03:02:37 pm
Code: [Select]
get_num = function();print("Enter number: ");max_value = io.read("*number");end
if type(max_value) == number;for i=1,max_value,1 do print(i) end
else;print("Not a Number!");break;end

The above code is a simple way of doing this -- it accepts only numbers through io.read, but checks again in case of interpretation error.
Title: Re: Lua Loop Defined By User
Post by: Munchor on May 31, 2011, 04:28:10 pm
That code gave this to me:

Code: [Select]
lua: helloworld.lua:1: unexpected symbol near ';'
Title: Re: Lua Loop Defined By User
Post by: Ashbad on May 31, 2011, 04:32:29 pm
sometimes Lua can be annoying like that.  Try this:

Code: [Select]
get_num = function()
  print("Enter number: ")
  max_value = io.read("*number")
end
if type(max_value) == number
  for i=1,max_value,1 do print(i) end
else
  print("Not a Number!")
  break
end
Title: Re: Lua Loop Defined By User
Post by: Munchor on May 31, 2011, 04:36:45 pm
That code gave:

Code: [Select]
lua: helloworld.lua:6: 'then' expected near 'for'
I'm getting a bit pissed at Lua, the syntax is confusing.
Title: Re: Lua Loop Defined By User
Post by: FinaleTI on May 31, 2011, 04:56:14 pm
Code: [Select]
get_num = function()
  print("Enter number: ")
  max_value = io.read("*number")
end
if type(max_value) == number then
  for i=1,max_value,1 do print(i) end
else
  print("Not a Number!")
  break
end
I only just started messing with Lua, but perhaps this will fix it?
(Put a then right after the if)
Title: Re: Lua Loop Defined By User
Post by: Munchor on May 31, 2011, 04:59:26 pm
Code: [Select]
get_num = function()
  print("Enter number: ")
  max_value = io.read("*number")
end
if type(max_value) == number then
  for i=1,max_value,1 do print(i) end
else
  print("Not a Number!")
end
This returns "Not a Number!".

Code: [Select]
get_num = function()
  print("Enter number: ")
  max_value = io.read("*number")
end
if type(max_value) == number then
  for i=1,max_value,1 do print(i) end
else
  print("Not a Number!")
  break
end

This returns "lua: helloworld.lua:10: no loop to break near 'end'."
Title: Re: Lua Loop Defined By User
Post by: BrownyTCat on May 31, 2011, 04:59:39 pm
I'm more fluent in Lua syntax than most other languages, it's easy when you memorize it.
Title: Re: Lua Loop Defined By User
Post by: Munchor on May 31, 2011, 04:59:59 pm
I'm more fluent in Lua syntax than most other languages, it's easy when you memorize it.

Then perhaps you can help me please?
Title: Re: Lua Loop Defined By User
Post by: BrownyTCat on May 31, 2011, 05:01:17 pm
I'm more fluent in Lua syntax than most other languages, it's easy when you memorize it.

Then perhaps you can help me please?
The problem I have is the nSpire platform, I can't even make a loop to draw a screen.
Title: Re: Lua Loop Defined By User
Post by: Munchor on May 31, 2011, 05:11:42 pm
I'm more fluent in Lua syntax than most other languages, it's easy when you memorize it.

Then perhaps you can help me please?
The problem I have is the nSpire platform, I can't even make a loop to draw a screen.

Perhaps I should link you to the forum where this topic is.

Right here (http://www.omnimaga.org/index.php?board=181.0).
Title: Re: Lua Loop Defined By User
Post by: FinaleTI on May 31, 2011, 05:16:50 pm
Code: [Select]
  print("Enter number: ")
  max_value = io.read("*number")
  if type(max_value) == "number" then
  for i=1,max_value,1 do print(i) end
  else
  print("Not a Number!")
  end
This seems to work.
Title: Re: Lua Loop Defined By User
Post by: Munchor on May 31, 2011, 05:18:16 pm
Code: [Select]
  print("Enter number: ")
  max_value = io.read("*number")
  if type(max_value) == "number" then
  for i=1,max_value,1 do print(i) end
  else
  print("Not a Number!")
  end
This seems to work.

Indeed it works, thanks a lot :D
Title: Re: Lua Loop Defined By User
Post by: FinaleTI on May 31, 2011, 05:20:09 pm
No prob, it was a learning experience for me too.
Title: Re: Lua Loop Defined By User
Post by: Munchor on May 31, 2011, 05:43:25 pm
Phew, after a while, I managed to make my first program

Code: [Select]
print("Enter number: ")
max_value = io.read("*number")           --Get maximum random number
if type(max_value) == "number" then      --If the user enters a number

  math.randomseed( os.time() )
  random_number = math.random(max_value) --Define a number
 
  while true do
    print ("Guess a number: ")
    tried_number = io.read("*number")
    if type(tried_number) == "number" then
      if tried_number == random_number then
        print ("You Won")
        break
      end
    end
  end

else
  print("Not a Number!")
  end

I will still add a few checks and other stuff, but this language is not really for me.

EDIT:

Yay, added a few cool stuff:

Code: [Select]
print("Enter number: ")
max_value = io.read("*number")           --Get maximum random number
if type(max_value) == "number" then      --If the user enters a number

  math.randomseed( os.time() )
  random_number = math.random(max_value) --Define a number
 
  while true do
    print ("Guess a number: ")
    tried_number = io.read("*number")
    if type(tried_number) == "number" then
      if tried_number == random_number then
        print ("You Won")
        break
      elseif tried_number > random_number then
        print ("Try a lower number")
      else
        print ("Try a higher number")
      end
    else
      print ("Not a number!")
    end
  end

else
  print("Not a number!")
  end
Title: Re: Lua Loop Defined By User
Post by: ruler501 on May 31, 2011, 07:11:08 pm
we have our WFRNG already for Lua. Are you going to port this to nspire lua?
Title: Re: Lua Loop Defined By User
Post by: Ashbad on May 31, 2011, 07:14:44 pm
Lua is a highly flexible language in many namable aspects, but when it comes to me writing condensing code "Ashbad-style" (AKA, as small as close as possible while still retaining meaning and without crossing the evil pink line in NetBeans Ruby) it's really picky, unfortunately :P
Title: Re: Lua Loop Defined By User
Post by: Munchor on June 01, 2011, 02:46:12 am
we have our WFRNG already for Lua. Are you going to port this to nspire lua?

This is not a WFRNG. This is a number guesser, in WFRNG there is no random number, it's the last one you choose.
Title: Re: Lua Loop Defined By User
Post by: JosJuice on June 01, 2011, 04:42:26 am
we have our WFRNG already for Lua. Are you going to port this to nspire lua?

This is not a WFRNG. This is a number guesser, in WFRNG there is no random number, it's the last one you choose.
No, WFRNG does generate random numbars (although 29 is quite common). The Lua version might be different, though...
Title: Re: Lua Loop Defined By User
Post by: Munchor on June 01, 2011, 08:35:32 am
Code: [Select]
--Program made by David Gomes

print("Enter number: ")
max_value = io.read("*number")           --Get maximum random number
if type(max_value) == "number" then      --If the user enters a number

  math.randomseed( os.time() )
  random_number = math.random(max_value) --Define a number
 
  counter = 0
 
  while true do
    print ("Guess a number: ")
    tried_number = io.read("*number")
    if type(tried_number) == "number" then
      if tried_number == random_number then
        print ("You Won")
        print (counter)
        break
      elseif tried_number > random_number then
        print ("Try a lower number")
        counter = counter + 1
      else
        print ("Try a higher number")
        counter = counter + 1
      end
    else
      print ("Not a number!")
    end
  end

else
  print("Not a number!")
  end

I actually thought that in a WFRNG, when counter=29, then it's right.

Here's a Lua program that is a number guesser, you win when you try the last option.

Example:

Code: [Select]
Max Value = 5
0
No
1
No
2
No
4
No
5
No
3
Yes

Code:

Code: [Select]
--Program made by David Gomes

print("Enter number: ")
max_value = io.read("*number")           --Get maximum random number
if type(max_value) == "number" then      --If the user enters a number

  math.randomseed( os.time() )
  random_number = math.random(max_value) --Define a number
 
  counter = 0
 
  while true do
    print ("Guess a number: ")
    tried_number = io.read("*number")
    if type(tried_number) == "number" then
      if counter<(max_value) then
        print ("Wrong Number")
        counter = counter + 1
      else
        print ("You won")
        break
      end
    else
      print ("Not a number!")
    end
  end

else
  print("Not a number!")
  end
Title: Re: Lua Loop Defined By User
Post by: JosJuice on June 01, 2011, 10:41:53 am
The thing with WFRNG on 8x calcs is that the inital RNG seed always produces 29. If you run some random stuff (and by that, I mean programs that use the RNG) before WFRNG, you probably won't get 29.
Title: Re: Lua Loop Defined By User
Post by: Jim Bauwens on June 02, 2011, 05:43:26 am
Here is David's program ported to run on my nspire  ;D

Title: Re: Lua Loop Defined By User
Post by: ruler501 on June 02, 2011, 08:24:33 am
Yay, I still say its WFRNG