简体   繁体   English

lua 代码工作,但在代码完成之前停止

[英]lua code working, but stops before code is finished

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.我正在为 lua 编写用户输入代码,如果您在输入信息时出错,您可以通过说出您要修复的内容来修复它。 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.在你输入这两个字母后(例如说你拼错了 england 并拼写为 englaund,你可以输入 HT 来修复它。)它会提示你修复它,并且在你这样做之后它只是说代码已完成,即使它不是。

I have tried making the variables local, making the blocks all ifs and not elseifs.我试过使变量成为本地变量,使块都是 ifs 而不是 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.我希望它打印出“好的,输入...的更正版本”,但它甚至没有用。

You might want to change answer3 == VG to answer3 == "VG" (and the others as well). 您可能需要将answer3 == VG更改为answer3 == "VG" (以及其他)。 Currently, it is comparing with a variable by the name VG , which presumably doesn't exist. 当前,它正在与名称为VG的变量进行比较,该变量可能不存在。

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.' 在提示用户使用“ aww”输入“是”或“否”后'aww, I was wrong. Do you want to enter the information again? Say yes or no.' '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. 消息时,您询问需要进行哪些更改并将用户输入存储在answer3变量中。

But you are comparing the value of answer3 to other variables like FN , LN , etc instead of strings like "FN" and "LN" . 但是,您正在将answer3的值与其他变量(例如FNLN等)而不是类似"FN""LN"的字符串进行比较。

Lua doesn't complain about this as undefined variables are considered to have nil value. Lua对此并不抱怨,因为未定义的变量被认为具有nil值。


Also, you have used undefined variable answerVG when only FN or LN or HT is changed. 另外,仅更改FNLNHT时,您使用了未定义的变量answerVG Use the VideoGame variable instead. 请改用VideoGame变量。


When comparing the value of answer3 , instead of using distinct if .. end s, you could use an if-else ladder like 当比较值answer3而不是使用不同的, if .. end S,你可以使用if-else的阶梯状:

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

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.您还针对重新输入信息的每个条件使用单独的 if 语句,这会减慢程序的加载速度。

Instead you may want to put相反,你可能想把

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM