Author Topic: Lua Loop Defined By User  (Read 8077 times)

0 Members and 1 Guest are viewing this topic.

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Lua Loop Defined By User
« 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.

Ashbad

  • Guest
Re: Lua Loop Defined By User
« Reply #1 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.

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Lua Loop Defined By User
« Reply #2 on: May 31, 2011, 04:28:10 pm »
That code gave this to me:

Code: [Select]
lua: helloworld.lua:1: unexpected symbol near ';'

Ashbad

  • Guest
Re: Lua Loop Defined By User
« Reply #3 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

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Lua Loop Defined By User
« Reply #4 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.
« Last Edit: May 31, 2011, 04:36:53 pm by Scout »

Offline FinaleTI

  • Believe in the pony that believes in you!
  • CoT Emeritus
  • LV10 31337 u53r (Next: 2000)
  • *
  • Posts: 1830
  • Rating: +121/-2
  • Believe in the pony that believes in you!
    • View Profile
    • dmuckerman.tumblr.com
Re: Lua Loop Defined By User
« Reply #5 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)


Spoiler For Projects:

My projects haven't been worked on in a while, so they're all on hiatus for the time being. I do hope to eventually return to them in some form or another...

Spoiler For Pokemon TI:
Axe port of Pokemon Red/Blue to the 83+/84+ family. On hold.

Spoiler For Nostalgia:
My big personal project, an original RPG about dimensional travel and a few heroes tasked with saving the world.
Coding-wise, on hold, but I am re-working the story.

Spoiler For Finale's Super Insane Tunnel Pack of Doom:
I will be combining Blur and Collision Course into a single gamepack. On hold.

Spoiler For Nostalgia Origins: Sky's Story:
Prequel to Nostalgia. On hold, especially while the story is re-worked.

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Lua Loop Defined By User
« Reply #6 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'."

Offline BrownyTCat

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 420
  • Rating: +37/-8
    • View Profile
Re: Lua Loop Defined By User
« Reply #7 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.

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Lua Loop Defined By User
« Reply #8 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?

Offline BrownyTCat

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 420
  • Rating: +37/-8
    • View Profile
Re: Lua Loop Defined By User
« Reply #9 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.

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Lua Loop Defined By User
« Reply #10 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.

Offline FinaleTI

  • Believe in the pony that believes in you!
  • CoT Emeritus
  • LV10 31337 u53r (Next: 2000)
  • *
  • Posts: 1830
  • Rating: +121/-2
  • Believe in the pony that believes in you!
    • View Profile
    • dmuckerman.tumblr.com
Re: Lua Loop Defined By User
« Reply #11 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.


Spoiler For Projects:

My projects haven't been worked on in a while, so they're all on hiatus for the time being. I do hope to eventually return to them in some form or another...

Spoiler For Pokemon TI:
Axe port of Pokemon Red/Blue to the 83+/84+ family. On hold.

Spoiler For Nostalgia:
My big personal project, an original RPG about dimensional travel and a few heroes tasked with saving the world.
Coding-wise, on hold, but I am re-working the story.

Spoiler For Finale's Super Insane Tunnel Pack of Doom:
I will be combining Blur and Collision Course into a single gamepack. On hold.

Spoiler For Nostalgia Origins: Sky's Story:
Prequel to Nostalgia. On hold, especially while the story is re-worked.

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Lua Loop Defined By User
« Reply #12 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

Offline FinaleTI

  • Believe in the pony that believes in you!
  • CoT Emeritus
  • LV10 31337 u53r (Next: 2000)
  • *
  • Posts: 1830
  • Rating: +121/-2
  • Believe in the pony that believes in you!
    • View Profile
    • dmuckerman.tumblr.com
Re: Lua Loop Defined By User
« Reply #13 on: May 31, 2011, 05:20:09 pm »
No prob, it was a learning experience for me too.


Spoiler For Projects:

My projects haven't been worked on in a while, so they're all on hiatus for the time being. I do hope to eventually return to them in some form or another...

Spoiler For Pokemon TI:
Axe port of Pokemon Red/Blue to the 83+/84+ family. On hold.

Spoiler For Nostalgia:
My big personal project, an original RPG about dimensional travel and a few heroes tasked with saving the world.
Coding-wise, on hold, but I am re-working the story.

Spoiler For Finale's Super Insane Tunnel Pack of Doom:
I will be combining Blur and Collision Course into a single gamepack. On hold.

Spoiler For Nostalgia Origins: Sky's Story:
Prequel to Nostalgia. On hold, especially while the story is re-worked.

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Lua Loop Defined By User
« Reply #14 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
« Last Edit: May 31, 2011, 05:46:07 pm by Scout »