This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
zeldafan99's profile picture

if intuput = anything but an input listed

Started by zeldafan99, 23 April 2012 - 06:36 PM
zeldafan99 #1
Posted 23 April 2012 - 08:36 PM
so um… how exactly do i edit this code so:

the user inputs anything other then a valid input, X happens


for example

is minecraft cool?
y
n
>

*user types* "asdfphdsf"

print ("what?")

zeldafan99 #2
Posted 23 April 2012 - 08:37 PM
also *input , for some reason firefox's spell check doesnt work on this site.
OminousPenguin #3
Posted 23 April 2012 - 09:16 PM
if input=="y" then
  .....
elseif input=="n" then
  .....
else
  print("what?")
end

Also, you can edit your post. No need to double post.
Luanub #4
Posted 23 April 2012 - 09:17 PM
Bah ninja'd :)/>/> ….

Just an if elseif else statement. so..


while true do
print ("Is minecraft cool??")
input = read()
if input == y then
   print ("woot!!")
elseif input == n thne
   print ("your lame")
else
   print ("huh? Try again..")
end
end
Edited on 23 April 2012 - 07:17 PM
Advert #5
Posted 23 April 2012 - 09:28 PM
I'd recommend using a table as a set over an elseif chain for this.

local valid = {
 ['y'] = true,
 ['n'] = true
}

local input = read()
if not valid[input] then
 -- bad input
end
...