Omnimaga
General Discussion => Technology and Development => Computer Programming => Topic started 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
Number: 2
Running through loop : 0
Running through loop : 1
Now, i can print and read from user:
print ("Enter number: ")
max_value = io.read()
My problem is looping max_value number of times, thanks.
-
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.
-
That code gave this to me:
lua: helloworld.lua:1: unexpected symbol near ';'
-
sometimes Lua can be annoying like that. Try this:
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
-
That code gave:
lua: helloworld.lua:6: 'then' expected near 'for'
I'm getting a bit pissed at Lua, the syntax is confusing.
-
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)
-
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!".
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'."
-
I'm more fluent in Lua syntax than most other languages, it's easy when you memorize it.
-
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?
-
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.
-
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).
-
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.
-
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
-
No prob, it was a learning experience for me too.
-
Phew, after a while, I managed to make my first program
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:
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
-
we have our WFRNG already for Lua. Are you going to port this to nspire lua?
-
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
-
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.
-
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...
-
--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:
Max Value = 5
0
No
1
No
2
No
4
No
5
No
3
Yes
Code:
--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
-
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.
-
Here is David's program ported to run on my nspire ;D
-
Yay, I still say its WFRNG