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

Something wrong on simple code

Started by PedroBarbosa, 21 December 2012 - 08:43 AM
PedroBarbosa #1
Posted 21 December 2012 - 09:43 AM
This is someting srong on this code… can you fix it?



print("Ready to start?")
write("=> ")
ready = io.read()
if ready == "Yes" or "yes" then
print("Your choice is yes")
elseif ready == "No" or "no" then
print("Your choice is no")
else
print("Wrong choice")
end
Orwell #2
Posted 21 December 2012 - 09:57 AM
This will always return true, because you check if ready equals "Yes" and if "yes" exists (which it always does)

if ready == "Yes" or "yes" then

Use this instead:

print("Ready to start?")
write("=> ")
ready = io.read():lower()
if ready == "yes" then
print("Your choice is yes")
elseif ready == "no" then
print("Your choice is no")
else
print("Wrong choice")
end

io.read():lower() will convert the input to all lower case characters.
remiX #3
Posted 21 December 2012 - 11:29 AM
You code will work fine but with the or's you need to do this:
if ready == "Yes" or ready == "yes" then
Orwell #4
Posted 21 December 2012 - 11:39 AM
Of course, but in this case string.lower() is good practice as it also accepts 'YES' and the sorts.