我正在为lua做用户输入代码,如果您在输入信息时出错,则可以通过说出您要修复的内容来进行修复。键入两个字母后(例如,说您拼写错误的英国字母并将其拼写为englaund,则可以键入HT对其进行修复。)它提示您对其进行修复,执行完后它只是说代码已完成,即使它不是。
我尝试使变量成为局部变量,使块全部为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
我希望它能打印“好,放上...的正确版本”,但什至没有用。
答案 0 :(得分:1)
您可能希望将answer3 == VG
更改为answer3 == "VG"
(以及其他)。当前,它正在与名称为VG
的变量进行比较,该变量可能不存在。
答案 1 :(得分:0)
在提示用户使用'aww, I was wrong. Do you want to enter the information again? Say yes or no.'
消息输入“是”或“否”之后,您询问需要进行哪些更改并将用户输入存储在answer3
变量中。
但是您正在将answer3
与其他变量(例如FN
,LN
等)的值进行比较,而不是像"FN"
和{ {1}}。
Lua对此并不抱怨,因为未定义的变量被认为具有"LN"
值。
此外,当仅更改nil
或answerVG
或FN
时,您使用了未定义的变量LN
。请改用HT
变量。
比较VideoGame
的值时,可以使用if-else阶梯之类的方法,而不是使用不同的answer3
,
if .. end