This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
CantCode420's profile picture

Help with lua.

Started by CantCode420, 15 June 2016 - 03:04 PM
CantCode420 #1
Posted 15 June 2016 - 05:04 PM
So I am trying to write a program on lua. I can't find any lua forums, so this is the closest I can get. My code is not written in computercraft, really sorry. Here it is. And can someone tell me about a lua help site?

hello = io.read()
print("Hi!")
print('What is Your Name?')
name = io.read()
print('Hello, ' .. name .. '!')
print("Let us have a conversation!")

print("Alright! What would you like to talk about?")

jokes = io.read()

print("What is a dentists favorite time?")
When = io.read()

print("2:30!")
print("Would you like to talk more?")
more = io.read()

if more == "Yes" then
print("Ok! What would you like to talk about?")
no = io.read()
if no == "No" then

end

end
print("Goodbye!")

os.execute("cls")
Movies = io.read()
if Movies == "Movies" then
print("What movies have you watched recently?")
end
io.read()
what it is supposed to do is be a chatbot. What it does is in the final line, it is supposed to say "What movies have you seen recently?" Instead I have to type "Movies" twice. The first it pops up "Goodbye!" Please help.
Lupus590 #2
Posted 15 June 2016 - 06:32 PM
https://www.lua.org/start.html#help

also
 tags

I had a look though and couldn't find your bug, the [code] tags may help with that though
Edited on 15 June 2016 - 04:33 PM
KingofGamesYami #3
Posted 15 June 2016 - 06:33 PM
For pure lua stuff, you can ask here (unless it's 5.2 or 5.3). Otherwise, I'd recommend Stack Overflow. It's great for pretty much any language you care to think of.

As for your program, perhaps a bit more information? I would expect this to happen:


>Hello
Bot>Hi!
Bot>What is your name?
>My Name
Bot>Hello, My Name!
Bot>Let's have a conversation
Bot>Alright, what would you like to talk about?
>Food
Bot>What is a dentists favorite time?
>No, I want to talk about food!
Bot>2:30!"
Bot>Would you like to talk more?
>Yes
Bot>Ok!  What would you like to talk about?
>Food
Bot>Goodbye!
>Movies
Bot>What movies have you watched recently?
>I like pie
Dog #4
Posted 15 June 2016 - 06:39 PM
Every time you call io.read() your script is going to stop and wait for input. You're being asked twice near the end because you're asking twice. First with no = io.read() then with Movies = io.read().

That chunk of code should look something like this…

more = io.read()
if more == "Yes" then --# if more equals "Yes" then do the following
  print("OK! What would you like to talk about?")
elseif more == "No" then --# if more equals "No" then do the following
  print("Goodbye")
  return --# return to whatever called this - since nothing called this and it's just an in-line script, this will essentially exit - if you don't do this, the script will continue on to the movies question
end
os.execute("cls")
Movies = io.read()
Edited on 15 June 2016 - 04:40 PM
CantCode420 #5
Posted 16 June 2016 - 12:19 AM
Every time you call io.read() your script is going to stop and wait for input. You're being asked twice near the end because you're asking twice. First with no = io.read() then with Movies = io.read().

That chunk of code should look something like this…

more = io.read()
if more == "Yes" then --# if more equals "Yes" then do the following
  print("OK! What would you like to talk about?")
elseif more == "No" then --# if more equals "No" then do the following
  print("Goodbye")
  return --# return to whatever called this - since nothing called this and it's just an in-line script, this will essentially exit - if you don't do this, the script will continue on to the movies question
end
os.execute("cls")
Movies = io.read()
I dont understand. Can you dumb it down for me?
Saldor010 #6
Posted 16 June 2016 - 12:30 AM
I dont understand. Can you dumb it down for me?

What exactly do you not understand? The way he's changing the if statement?
Dog #7
Posted 16 June 2016 - 12:35 AM
I dont understand. Can you dumb it down for me?

Not really - an if/elseif statement is almost as basic as it gets. What exactly are you unclear about?

To recap - every time you call io.read() your script is going to stop and wait for input - look at your script, look at what I posted, and see if you can figure why you're having to type "Movies" twice. If you re-read what I posted and go through your script step by step it should be pretty obvious.

Basically, here's what happening in that portion of your script:

> Would you like to talk more?
> "Yes" – your script stops here waiting for more = io.read() - in this example, the user typed "Yes"
> Ok! What would you like to talk about?
>> your script stops here waiting for no = io.read(), you don't need this, you've already asked whether the user wants to continue with more = io.read() - you need to parse more for a "Yes" or a "No", not re-ask the question again. In your code, you're only checking if more equals "Yes", you're not checking if it equals "No", then you're essentially re-asking the question with no = io.read()
Edited on 15 June 2016 - 10:50 PM
CantCode420 #8
Posted 16 June 2016 - 04:06 PM
Thanks! Worked like a dream!