Need help with my Lua Loop

Hello dear Community,

I try to learn lua but I have a problem with my code.

print("Choose your number") local number = io.read("\*n") repeat if number == 100 then print("Max. Number") elseif zahl \< 100 then print("Number is less then 100") else print("Wrong!") end print("Restart the program?") answer = io.read() until answer ~= "No"

When I run my program then I can not make a user input.

My output is:

Choose your number

>10

Number is less then 100

Restart the program?

Can someone help me?

Are you setting the io.input file and path correctly? If not, your variable “number” will just be nil. I suspect that is the problem.

Yes the path is correct. I made it with a online IDE and there’s the same problem. I can’t use the io.read .

Since you’re using “*n” in io.read(), you’re expecting to read a number from the console / terminal. Not sure where the variable ‘zahl’ comes from so I changed it in the code below. Also, comparing a number with a string would throw an error; so I’m comparing with zero at the end:

-- moved the question and input inside the loop repeat &nbsp;&nbsp;&nbsp; print("Choose your number") &nbsp;&nbsp;&nbsp; local answer = io.read("\*n") &nbsp;&nbsp;&nbsp; if answer == 100 then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print("Max. Number") &nbsp;&nbsp;&nbsp; elseif answer \< 100 then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print("Number is less than 100") &nbsp;&nbsp;&nbsp; else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print("Wrong!") &nbsp;&nbsp;&nbsp; end until answer == 0

I’m puzzled here. What do you mean with “I can not make a user input?”. What exactly is the error or problem here, because I’m not following.

You’re not asking for user input in the code above. You are reading a file. And if the number in that file is lower than 100 everything seems to work as it should, no?

By the way, put

print(number)

right after the line:

local number = io.read("*n")

and let us know what happens here. Also, make sure you are not treating a string as a number.

I like to restart the code when the code asks me if I like to restart the program. When I write Yes then it should restart the program.

Hi,

It might be a language issue, but I think you will get more help if you ask your question in a clearer way. I would like to help, but I don’t know how: first you ask about help with the loop, now it’s about restarting the code when you write “yes”, and even that is very unclear: what do you mean “writing yes”? Do you mean typing “yes” on a keyboard, or touching a button, or something else?

Also, make sure to include all relevant code. For example, what is the value “zahl” etc…

If you ask your question better, you will get more help and better answers.

Hi,

sry for my bad english first. The value zahl should be number not zahl, zahl is german. I’d like to make a program who will restart the code in the loop when I write yes in the console. It should be like this:

Do you like to restart the program? And if I type in “yes” then it should restart my code in the loop.

Hi Mehmet,

I might be wrong, but as far as I know you can not type into the Corona console and use it as input. At least I’ve never read or heard anyone on this forum that used this technique, so I’m 99 percent sure.

That means that you will need to use another form of input. For a simple YES / NO question I think two buttons would be the simplest option (if you are developing for a device with a touch screen).

I have some more questions: what exactly do you want you application to do? I you can give me a bit more answers I’ll try to write you some demo code.

Cheers and auf wiedersehn!

Lua as a language be used in multiple ways. One way is running Lua from the command line at which you can do keyboard input using io.read(). However Corona SDK uses Lua in a way that you cannot use io.read() to read the keyboard.

I believe the OP is using the command line Lua or using it in some non-Corona SDK tool. Which of course, we can’t provide support for. But his technique for learning lua is perfectly valid.

To summarize:

Corona SDK cannot use  io.read() for keyboard input.

Command line “Lua” can.

Rob

Are you setting the io.input file and path correctly? If not, your variable “number” will just be nil. I suspect that is the problem.

Yes the path is correct. I made it with a online IDE and there’s the same problem. I can’t use the io.read .

Since you’re using “*n” in io.read(), you’re expecting to read a number from the console / terminal. Not sure where the variable ‘zahl’ comes from so I changed it in the code below. Also, comparing a number with a string would throw an error; so I’m comparing with zero at the end:

-- moved the question and input inside the loop repeat &nbsp;&nbsp;&nbsp; print("Choose your number") &nbsp;&nbsp;&nbsp; local answer = io.read("\*n") &nbsp;&nbsp;&nbsp; if answer == 100 then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print("Max. Number") &nbsp;&nbsp;&nbsp; elseif answer \< 100 then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print("Number is less than 100") &nbsp;&nbsp;&nbsp; else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print("Wrong!") &nbsp;&nbsp;&nbsp; end until answer == 0

I’m puzzled here. What do you mean with “I can not make a user input?”. What exactly is the error or problem here, because I’m not following.

You’re not asking for user input in the code above. You are reading a file. And if the number in that file is lower than 100 everything seems to work as it should, no?

By the way, put

print(number)

right after the line:

local number = io.read("*n")

and let us know what happens here. Also, make sure you are not treating a string as a number.

I like to restart the code when the code asks me if I like to restart the program. When I write Yes then it should restart the program.

Hi,

It might be a language issue, but I think you will get more help if you ask your question in a clearer way. I would like to help, but I don’t know how: first you ask about help with the loop, now it’s about restarting the code when you write “yes”, and even that is very unclear: what do you mean “writing yes”? Do you mean typing “yes” on a keyboard, or touching a button, or something else?

Also, make sure to include all relevant code. For example, what is the value “zahl” etc…

If you ask your question better, you will get more help and better answers.

Hi,

sry for my bad english first. The value zahl should be number not zahl, zahl is german. I’d like to make a program who will restart the code in the loop when I write yes in the console. It should be like this:

Do you like to restart the program? And if I type in “yes” then it should restart my code in the loop.

Hi Mehmet,

I might be wrong, but as far as I know you can not type into the Corona console and use it as input. At least I’ve never read or heard anyone on this forum that used this technique, so I’m 99 percent sure.

That means that you will need to use another form of input. For a simple YES / NO question I think two buttons would be the simplest option (if you are developing for a device with a touch screen).

I have some more questions: what exactly do you want you application to do? I you can give me a bit more answers I’ll try to write you some demo code.

Cheers and auf wiedersehn!

Lua as a language be used in multiple ways. One way is running Lua from the command line at which you can do keyboard input using io.read(). However Corona SDK uses Lua in a way that you cannot use io.read() to read the keyboard.

I believe the OP is using the command line Lua or using it in some non-Corona SDK tool. Which of course, we can’t provide support for. But his technique for learning lua is perfectly valid.

To summarize:

Corona SDK cannot use  io.read() for keyboard input.

Command line “Lua” can.

Rob