Jump to content




make read() all lower case?


  • You cannot reply to this topic
6 replies to this topic

#1 subzero22

  • Members
  • 97 posts

Posted 06 January 2014 - 09:15 PM

Is there a way to make it so when someone has to type something in it makes it all lower case in case they put some caps in there?

here's a snip of the code I'm using.

print("Build left or right?")
write("> ")
left = read()
print("")

print("would you like the turlte to dig the floor also? yes/no")
write("> ")
dig = read()
print("")

full program incase you want to see it http://pastebin.com/Y9ZwQkgq

#2 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 06 January 2014 - 10:18 PM

string.lower takes a string and returns an all-lower-case version of that string.

#3 oeed

    Oversimplifier

  • Members
  • 2,095 posts
  • LocationAuckland, New Zealand

Posted 06 January 2014 - 10:39 PM

Going on from what Lyqyd said,

You can use string.lower, so for example,

print("Build left or right?")
write("> ")
left = string.lower(read())
print("")

If you want it to be lowercase as you type (for example, if you typed 'Apple' it would display 'apple') you'd need to modify the read function and use string.lower on the character input.

#4 subzero22

  • Members
  • 97 posts

Posted 07 January 2014 - 02:42 AM

ok thanks for the help :)

#5 subzero22

  • Members
  • 97 posts

Posted 07 January 2014 - 05:09 AM

sadly I cant test right now but would I also need to change the if statements or would they stay the same?

I also have another question but kinda off topic. For turtle event types for like a refuel script could I do something like this?

if turtle.getFuelLevel() <= fuel then
print("Turtle has "..flvl.." fuel and needs "..fuel.."fuel to strip this much.")
print("Please put fuel in slot 1.")

-- would I use this like in the wiki?

os.pullEvent("turtle_inventory")

-- or would I have to add something else?

turtle.refuel(64)
else
print("Stripmine Starting")
sleep(1)
end


#6 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 07 January 2014 - 05:36 AM

You might want to throw in a turtle.select() to make sure slot 1 is selected (for eg, remember that restarting the script won't reset the current slot), and maybe a while loop to ensure it actually gets refueled, but yeah, the line works more or less as you seem to think it does.

turtle.select(1)
if turtle.getFuelLevel() < fuel then
  print("Turtle has "..turtle.getFuelLevel().."/"..fuel.." required fuel.")
  turtle.refuel()

  while turtle.getFuelLevel() < fuel then
    -- After the last attempt to refuel, we still don't have enough.
    print("Please put more fuel in slot 1 (needs "..(fuel-turtle.getFuelLevel()).." more).")
    os.pullEvent("turtle_inventory")
    turtle.refuel()
  end
end

print("Have "..turtle.getFuelLevel().."/"..fuel.." fuel, stripmine starting...")

Regarding the "if" statements, since you can be sure the "left" variable holds a lower-case string, there's no need to pull anything fancy there. This is fine for eg:

left = string.lower(read())
if left == "left" then
.
.
.


#7 subzero22

  • Members
  • 97 posts

Posted 07 January 2014 - 06:28 PM

ok thanks





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users