Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
  • 0

Question

local robot = require "robot"
local term = require "term"

term.write("Please enter password: ")
a = term.read()
print(a)
if a == 2 then
  print("Meh")
end

So when I run the code it obviously asks for my password, so I might write 2, but then it just prints 2 and doesn't print Meh, it knows the value is 2 but the if then statement won't work. 

2017-09-30_12.58.56.png

Link to post
Share on other sites

8 answers to this question

Recommended Posts

  • 0

Hmm, interesting. If I make a new code:

local robot = require "robot"
local term = require "term"

term.write("Please enter password: ")
a = term.read()
print(a)
b = a+1
print(b)
if b == 2 then
  print("Meh")
end

This works, if I write 1 it says meh

So as you can see in the picture in my original post there's an extra line where it didnt write anything. It must have something to do with that. Because when do a+1 it does not do that extra empty line. 

But I still need help, because I want to do this with letters instead, and there I can't just do +1.

 

 

local robot = require "robot"
local term = require "term" 
 
term.write("Please enter password: ")
a = io.read("*line")
print(a)
if a == 2 then
  print("Meh")
end

This actually doesn't make that extra line, but the if then statement still only works if I do

b = a+1 ; if b == 2 then

 

 

if a == 2 then

Doesn't work

Link to post
Share on other sites
  • 0
7 hours ago, Molinko said:

io.read and term.read both return a string. Use tonumber(a)==2

That only works for numbers, I already found a way to do that even though it isn't how it's supposed to be done (a = a+0)

I need to know how I do it for words.

F.x

local robot = require "robot"
local term = require "term" 
 
term.write("Please enter password: ")
a = term.read()
print(a)
if a == Meas then
  print("Meh")
end

tonumber does not work for words... but thank you for taking some of your time to answer.

 

btw I thought strings were just numbers?

Link to post
Share on other sites
  • 0

If you want it to work for all words/strings then use "1" instead of 1. In lua numbers and strings are not the same, ie 1 != "1". It seems that lua for some reason allows strings to be implicitly converted to numbers when using arithmetic operators(who do they think they are, JavaScript). So if your inputs aren't restricted to being numbers only then just compare with the number as a string(in quotes)

Link to post
Share on other sites
  • 0

So like this?

local robot = require "robot"
local term = require "term" 
 
term.write("Please enter password: ")
a = term.read()
print(a)
if a == "Meas" then
  print("Meh")
end

because that doesn't work... (neither did it work with if I replaced Meas with 1)

I don't think I exactly understood what you meant, maybe you could show me in code what you are saying?

Link to post
Share on other sites
  • 0

Ok the code in the end is:

local robot = require "robot"
local term = require "term" 
 
term.write("Please enter password: ")
a = io.read("*line")
print(a)
if a == "Op" then
  print("Meh")
end

So I needed both of your tips, the quotes and the io.read() however I can say that term.read({dobreak=false}) does not work. As seen in the code below

Thanks to both of you, the problem is solved.

 

term.read({dobreak=false}) does not work:

local robot = require "robot"
local term = require "term" 
 
term.write("Please enter password: ")
a = term.read({dobreak=false})
print(a)
if a == "Op" then
  print("Meh")
end

the result was this

/home # Test
Please enter password: OpOp

/home #

I only wrote Op once, it then printed Op again right after and it still made a new line.

 

But thanks for helping me!

Link to post
Share on other sites
  • 0

term.read with dobreak option is in the later versions of OpenOS. It may have not been available to you yet. The older version of term.read takes a list of option arguments instead of an options table. It can be used like so..

term.read(nil, false)

Dobreak is the second arg here, set to false, nil will default to dobreak bring true.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.