0

I am doing user input code for lua, and if you made an error typing the information, you can fix it by saying the thing you want to fix. After you type the two letters (for example say you spelt england wrong and spelt it englaund, you would type HT to fix it.) it prompts you to fix it, and after you do it it just says the code is finished even though it is not.

I have tried making the variables local, making the blocks all ifs and not elseifs.

--user input--
print('Hello, what is your name? ')
local name = io.read()
print('What is your last name?')
local LastName = io.read()
print('The place you live?')
local Hometown = io.read()
print('Lastly, what is your favourite video game?')
local VideoGame = io.read()

--Printing the information--
print(
  'You are ' .. name .. ' ' .. LastName ..
  ' you live in ' .. Hometown ..
  ' and your favourite video game is ' .. VideoGame .. '.'
)
print('right?')

-- confirmation --    
io.write("press 1 i was correct, and press 2 if i was wrong.")
answer = io.read()

if answer == "1" then
  print('Yay, I was correct!')

elseif answer == "2" then
  print('aww, I was wrong. Do you want to enter the information again?  Say yes or no.')

  local answer2 = io.read()

  if answer2 == "yes" then
    print('What would you like to change? Type either FN, LN, HT or VG to change which one you would like.')

    local answer3 = io.read()

    if answer3 == FN then
      io.write('Ok, please enter the corrected version of your first name.')
      answerFN = io.read()
      io.write('Here is the corrected version.')
      io.write(
        'You are ' .. answerFN .. ' ' .. LastName ..
        ' you live in ' .. Hometown ..
        ' and your favourite video game is ' .. answerVG .. '.'
      )
    end

    if answer3 == LN then
      print('Ok, please enter the corrected version of your last name.')
      answerLN = io.read()
      print('Here is the corrected version.')
      print(
        'You are ' .. name .. ' ' .. answerLN ..
        ' you live in ' .. Hometown ..
        ' and your favourite video game is ' .. answerVG .. '.'
      )
    end

    if answer3 == HT then
      print('Ok, please enter the corrected version of your hometown.')
      answerHT = io.read()
      print('Here is the corrected version.')
      print(
        'You are ' .. name .. ' ' .. LastName ..
        ' you live in ' .. answerHT ..
        ' and your favourite video game is ' .. answerVG .. '.'
      )
    end


    if answer3 == VG then
      print('Ok, please enter the corrected version of your favourite video game.')
      answerVG = io.read()
      print('Here is the corrected version.')
      print(
        'You are ' .. name .. ' ' .. LastName ..
        ' you live in ' .. Hometown ..
        ' and your favourite video game is ' .. answerVG .. '.'
      )
    end

    if answer2 == "no" then
      print('Alright, tough luck. You can run the code again if you change your mind.')
    end
  end
end

I expected it to print 'ok, put the corrected version of ...', but it didn't even work.

1
  • Firstly there is an end too much, better use elseif for answer2 == 'no'. In this case you would never join the clause. Secondly the variables you are proofing answer3 for don't exist. Maybe you forgot the string quotes, for instance answer3 == 'FN'. Additionally you forgot the local var definition for answerFN, answerLN, ... So make sure to define them if you didn't already before. At least i would recommend you to write an function to print this standard string output (You are bla bla you live in ...'). Commented May 2, 2019 at 7:59

3 Answers 3

1

You might want to change answer3 == VG to answer3 == "VG" (and the others as well). Currently, it is comparing with a variable by the name VG, which presumably doesn't exist.

Sign up to request clarification or add additional context in comments.

Comments

1

You seem to be using variables instead of strings for reinputting info but the variables aren't set to anything. You are also using seperate if statements for each condition of reinputting info which can slow your program down loads.

Instead you may want to put

if answer3 == "FN" then
   ...
elseif answer3 == "LN" then
   ...
elseif answer3 == "HT" then
   ...
elseif answer3 == "VG" then
   ...
end

Comments

0

After you prompt the user to enter "yes" or "no" with the 'aww, I was wrong. Do you want to enter the information again? Say yes or no.' message, you ask what change is needed and store the user input in answer3 variable.

But you are comparing the value of answer3 to other variables like FN, LN, etc instead of strings like "FN" and "LN".

Lua doesn't complain about this as undefined variables are considered to have nil value.


Also, you have used undefined variable answerVG when only FN or LN or HT is changed. Use the VideoGame variable instead.


When comparing the value of answer3, instead of using distinct if .. ends, you could use an if-else ladder like

if <condition1> then
    ...
elseif <condition2> then
    ...
else
    ...
end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.