有没有办法进行更紧凑的答案检查
Is there a way to make a more compact answer check
我对编码很陌生,现在我的代码真的很庞大,我想知道是否有办法制作一个更紧凑的函数来检查答案,现在我只复制了 if then 语句和一遍又一遍地粘贴变量大写并每次都以不同的方式拼写,例如,对于 no 我有 N,n,no,NO,No,nO 的 if then 语句。
local men = io.read()
if men == "N" then
print(" You decide that you're fine with getting pushed around for your whole life, so you continue like that until you are old and die. THE END")
return
end
if men == "NO" then
print(" You decide that you're fine with getting pushed around for your whole life, so you continue like that until you are old and die. THE END")
return
end
if men == "no" then
print(" You decide that you're fine with getting pushed around for your whole life, so you continue like that until you are old and die. THE END")
return
end
if men == "No" then
print(" You decide that you're fine with getting pushed around for your whole life, so you continue like that until you are old and die. THE END")
return
end
if men == "n" then
print(" You decide that you're fine with getting pushed around for your whole life, so you continue like that until you are old and die. THE END")
return
end
您可以将选项放入列表中,然后通过 for 循环询问您的输入是否等于列表中的语句。如果不是就没有错误。
例如,如果您有如下列表:
l_words = ["N","n","no","NO","N0","nO"]
inp = input("Your input: ")
for i in l_words:
if inp != i:
print("true input")
else:
print("false input")
您可以使用 patterns。它们与大多数编程语言中使用的正则表达式非常相似。在这里,我测试答案字符串以查看它是否与您要查找的模式相匹配。下面是对模式的解释:
^
- 匹配字符串的开头,在此之前不允许有任何字符。如果您不包含它,那么它可以在字符串后面找到 'no',即 abcdNO
[nN]
- []
允许您包含可接受字符的列表,因此这里的第一个字符需要是 n
或 N
[oO]?
- 下一个字符必须是 o
或 O
,但 ?
表示它是可选的,它可以出现 0 次或 1 次。
$
匹配字符串的结尾,所以它不会匹配 'NOabcd` 因为你的模式后面不能有任何东西。
总之,这意味着字符串必须以 'n' 或 'N' 开头,可能只有一个 'o' 或 'O',之后没有其他内容。
string.find(string, pattern)
将查看字符串是否与模式匹配,return 它在字符串中找到的位置,如果未找到,则 nil
。
local answer = 'No'
local pattern = '^[nN][oO]?$'
if (string.find(answer, pattern) ~= nil) then
print('found!')
else
print('not found!')
end
您可以使用 set 然后检查输入是否是集合的成员。 Lua 中的一个简单集合可以这样定义:
local no = {
N = true,
n = true,
no = true,
NO = true,
No = true,
nO = true
}
你只需像任何索引一样使用它 table:
local men = io.read()
if no[men] then
print(" You decide that you're fine with getting pushed around for your whole life, so you continue like that until you are old and die. THE END")
return
end
a lua 中的 nil
在此上下文中将被视为 false
,您将从任何不是键的值中获得 nil
设置
我正在添加另一个答案以提供更简单的更改。您通常应该设法找到避免重复代码的方法。相同的 'print' 语句重复多次。
您可以做的一件事是使用 or operator 并将所有测试合并到一个表达式中。
local men = io.read()
if men == "N" or men == "NO" or men == "no" or men == "No" or men == "n" then
print(" You decide that you're fine with getting pushed around for your whole life, so you continue like that until you are old and die. THE END")
return
end
这是您所拥有的设置的最佳方法,但如果您没有像这样的简单条件并且想要重用代码,您可以使用重复的代码创建一个函数并调用它。假设您还想根据某些值做一些其他事情:
function badEnding()
print(" You decide that you're fine with getting pushed around for your whole life, so you continue like that until you are old and die. THE END")
-- NOTE: return is not required, the default return value will be nil
end
local men = io.read()
if men == "N" then return badEnding() end
if men == "NO" then
print("Hey, no need to shout!")
return badEnding()
end
if men == "no" then return badEnding() end
if men == "No" then return badEnding() end
if men == "n" then return badEnding() end
我对编码很陌生,现在我的代码真的很庞大,我想知道是否有办法制作一个更紧凑的函数来检查答案,现在我只复制了 if then 语句和一遍又一遍地粘贴变量大写并每次都以不同的方式拼写,例如,对于 no 我有 N,n,no,NO,No,nO 的 if then 语句。
local men = io.read()
if men == "N" then
print(" You decide that you're fine with getting pushed around for your whole life, so you continue like that until you are old and die. THE END")
return
end
if men == "NO" then
print(" You decide that you're fine with getting pushed around for your whole life, so you continue like that until you are old and die. THE END")
return
end
if men == "no" then
print(" You decide that you're fine with getting pushed around for your whole life, so you continue like that until you are old and die. THE END")
return
end
if men == "No" then
print(" You decide that you're fine with getting pushed around for your whole life, so you continue like that until you are old and die. THE END")
return
end
if men == "n" then
print(" You decide that you're fine with getting pushed around for your whole life, so you continue like that until you are old and die. THE END")
return
end
您可以将选项放入列表中,然后通过 for 循环询问您的输入是否等于列表中的语句。如果不是就没有错误。
例如,如果您有如下列表:
l_words = ["N","n","no","NO","N0","nO"]
inp = input("Your input: ")
for i in l_words:
if inp != i:
print("true input")
else:
print("false input")
您可以使用 patterns。它们与大多数编程语言中使用的正则表达式非常相似。在这里,我测试答案字符串以查看它是否与您要查找的模式相匹配。下面是对模式的解释:
^
- 匹配字符串的开头,在此之前不允许有任何字符。如果您不包含它,那么它可以在字符串后面找到 'no',即abcdNO
[nN]
-[]
允许您包含可接受字符的列表,因此这里的第一个字符需要是n
或N
[oO]?
- 下一个字符必须是o
或O
,但?
表示它是可选的,它可以出现 0 次或 1 次。$
匹配字符串的结尾,所以它不会匹配 'NOabcd` 因为你的模式后面不能有任何东西。
总之,这意味着字符串必须以 'n' 或 'N' 开头,可能只有一个 'o' 或 'O',之后没有其他内容。
string.find(string, pattern)
将查看字符串是否与模式匹配,return 它在字符串中找到的位置,如果未找到,则 nil
。
local answer = 'No'
local pattern = '^[nN][oO]?$'
if (string.find(answer, pattern) ~= nil) then
print('found!')
else
print('not found!')
end
您可以使用 set 然后检查输入是否是集合的成员。 Lua 中的一个简单集合可以这样定义:
local no = {
N = true,
n = true,
no = true,
NO = true,
No = true,
nO = true
}
你只需像任何索引一样使用它 table:
local men = io.read()
if no[men] then
print(" You decide that you're fine with getting pushed around for your whole life, so you continue like that until you are old and die. THE END")
return
end
a lua 中的 nil
在此上下文中将被视为 false
,您将从任何不是键的值中获得 nil
设置
我正在添加另一个答案以提供更简单的更改。您通常应该设法找到避免重复代码的方法。相同的 'print' 语句重复多次。
您可以做的一件事是使用 or operator 并将所有测试合并到一个表达式中。
local men = io.read()
if men == "N" or men == "NO" or men == "no" or men == "No" or men == "n" then
print(" You decide that you're fine with getting pushed around for your whole life, so you continue like that until you are old and die. THE END")
return
end
这是您所拥有的设置的最佳方法,但如果您没有像这样的简单条件并且想要重用代码,您可以使用重复的代码创建一个函数并调用它。假设您还想根据某些值做一些其他事情:
function badEnding()
print(" You decide that you're fine with getting pushed around for your whole life, so you continue like that until you are old and die. THE END")
-- NOTE: return is not required, the default return value will be nil
end
local men = io.read()
if men == "N" then return badEnding() end
if men == "NO" then
print("Hey, no need to shout!")
return badEnding()
end
if men == "no" then return badEnding() end
if men == "No" then return badEnding() end
if men == "n" then return badEnding() end