lua - lua 代码工作,但在代码完成之前停止
问题描述
我正在为 lua 做用户输入代码,如果您在输入信息时出错,您可以通过说出您想要修复的内容来修复它。在你输入这两个字母后(例如说你拼错了 england 并拼成了 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
我希望它会打印出“好的,放置...的更正版本”,但它甚至没有用。
解决方案
您可能想更改answer3 == VG
为answer3 == "VG"
(以及其他人)。目前,它正在与 name 的变量进行比较,该变量VG
可能不存在。
推荐阅读
- google-apps-script - 有没有办法让 Google 表格单元格在选中时显示多个超链接(工具提示中的每行一个)?
- python - 如何在 Beautiful Soup python 中阅读#document
- php - 未定义的属性:stdClass::$salarie
- linux - 如何使用 bash/awk 在列中显示我的脚本的输出?
- python - 将名称附加到许多 __init__*.py 是如何工作的?
- python - aiohttp客户端连接错误,但是我可以在网上找到网站就好了?
- c++ - 我想知道如何在边缘添加名称?我是 Dijkstra 算法的新手
- python-3.x - 虚拟环境不会在 Windows 10 上创建
- java - 如何使用来自 Firebase 实时数据库的电子邮件检索数据?
- batch-file - 在引号内连接 Windows 批处理文件命令中的变量和字符串