Pretty basic problem, just trying to print() text after code block regardless of previous code - printing

I am new to coding and I am trying to have a statement printed after my code, with other print() in the code block. I think I am having a problem with the indentation or I am not using the >>> or ... correctly
After the code I want it to say "move on" post what the code decides:
>>> age = 19
>>>if(age>18):
print("go through")
elif(age==18):
print("go see pink floyd")
else:
print("go to meat loaf")
print("move on")
So I would like it to say
"go through" or "go to pink floyd" or "go to meatloaf"
and then say "move on"
so if age is 19
go through
move on
if age is 18
go to pink floyd
move on
if age is 17
go to meatloaf
move on
but I keep getting a syntax error on based on my final print statement.
>>> age=19\
>>> if(age>18):\
print("go through")\
elif(age==18):\
print("go see pink floyd")\
else:\
print("go to meat loaf")\
print("move on")
SyntaxError: invalid syntax
Syntax error on the print("move on") statement
If I use the print statement in the else statement as follows:
>>> age=19\
>>> if(age>18):\
print("go through")\
elif(age==18):\
print("go see pink floyd")\
else:\
print("go to meat loaf")\
print("move on")
go through
I do not get the "move on" but it will work if the age<18
I want the the "move on" statement to be printed after the if, elif, else statement regardless.
I have also tried:
>>> age=19\
>>> if(age>18):\
print("go through")\
elif(age==18):\
print("go see pink floyd")\
else:\
print("go to meat loaf")\
>>> print("move on")
SyntaxError: invalid syntax\
So thank you for taking time to look at this, who ever that may be. I know its simple, but I greatly appreciate the help. I also tried some other version of the placement of the print statement and the "...print("move on"), but still get a syntax error.

Related

io.read() Doesn't let me input (Lua)

So im making a exponent calculator and i added a feauture which on the case the number is too big to be calculated it will instead just tell you how many digits it has. Here is my code:
io.write("Number is to big, calculate only number of digits instead? (Y/N): ")
local ans = io.read()
if ans:lower() == "y" then
--Irrelevant code (Doesnt get executed)
elseif ans:lower() == "n" then
--Irrelevant code
else
print("Error")
os.exit()
end
It will always print "Error" and end the program, any help?
Specification: Its not that i doesnt matter what i type, its that i doesnt even let me type, the io.read() doesnt execute andit prints error (note: this doesn't open files or something)
Second Specification: It prints error before im even able to type something, i dont even get to press enter, and in my code before that snippet i call io.read various times and it works fine

Is there a way to make a more compact answer check

I'm very new to coding and right now my code is really bulky and I want to know if there is a way to make a more compact function to check answers, right now I just have if then statements copied and pasted over and over with the variable capitalized and spelled a different way every time, for example, for no I have if then statements for N,n,no,NO,No,nO.
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
You can use patterns. They are very similar to regular expressions which are used across most programming languages. Here I test the answer string to see if it matches the pattern you're looking for. Here's an explanation of pattern:
^ - match the start of the string, don't allow any characters before this. If you don't include this then it could find 'no' later in the string, i.e. abcdNO
[nN] - the [] let you include a list of acceptable characters, so here the first character needs to be n or N
[oO]? - the next character has to be o or O, but the ? means it is optional, it can occur 0 or 1 times.
$ matches the end of the string, so it will not match 'NOabcd` because there can't be anything after your pattern.
In all that means the string has to start with 'n' or 'N', possibly have a single 'o' or 'O', and have nothing else after that.
string.find(string, pattern) will see if string is matched by pattern and return the position it was found in the string, or nil if not found.
local answer = 'No'
local pattern = '^[nN][oO]?$'
if (string.find(answer, pattern) ~= nil) then
print('found!')
else
print('not found!')
end
You can use a set then you check if the input is a member of the set. a simple set in Lua can be defined like so:
local no = {
N = true,
n = true,
no = true,
NO = true,
No = true,
nO = true
}
and you use it just by indexing it like any 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 nil in lua will be treated as a false in this context, and you will get nil from any value that is not a key in the set
I'm adding another answer to offer a simpler change. You should generally try to find a way to avoid duplicate code. The same 'print' statement is repeated several times.
One thing you can do is use the or operator and combine all your tests into one expression.
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
That is the best method for the setup you have, but if you don't have a simple condition like this and want to reuse the code you could create a function with the duplicated code and call it instead. Say if you wanted to do something ELSE as well depending on certain values:
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
You can put your options into a list and than ask through a for-loop if your input is equal to a statement in your list. If not there isn't a mistake.
For example if you have list like:
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")

How do I tell my app to read exactly what the string contains?

Stumbled on an issue I can't seem to figure out. I have an app that spits out a certain amount of points depending what the string is so if someone says "not 1" then my app will show -4 points for number 1. If someone says "its 2" then the app will award 12 points for 2.
So now my issue is, If someone says "its 2" and awards 12 points which is great, exactly what I want. But when someone says "its not 2" it still awards 12 points to 2 because it contains the word "its", when I want it to show -4 points.
|| vote.lowercased().contains("its")
|| vote.lowercased().contains("its not")
I want it to be able to see if it contains exactly "its not" then it will know to -4 points.
If you want to distinguish its and its not you have to check for the longer string first
if vote.lowercased().contains("its not") {
// -4 points
} else if vote.lowercased().contains("its") {
// 12 points
}

Lua - Couple Questions

Im a amatuer at coding. So, mind me if i face palmed some things.
Anyways, im making a alpha phase for a OS im making right? I'm making my installer. Two questions. Can i get a code off of pastebin then have my lua script download it? Two. I put the "print" part of the code in cmd. I get "Illegal characters". I dont know what went wrong. Here's my code.
--Variables
Yes = True
No = False
--Loading Screen
print ("1")
sleep(0.5)
print("2")
sleep(0.5)
print("Dowloading OS")
sleep(2)
print("Done!")
sleep(0.2)
print("Would you like to open the OS?")
end
I see a few issues with your code.
First of all, True and False are both meaningless names - which, unless you have assigned something to them earlier, are both equal to nil. Therefore, your Yes and No variables are both set to nil as well. This isn't because true and false don't exist in lua - they're just in lowercase: true and false. Creating Yes and No variables is redundant and hard to read - just use true and false directly.
Second of all, if you're using standard lua downloaded from their website, sleep is not a valid function (although it is in the Roblox version of Lua, or so I've heard). Like uppercase True and False, sleep is nil by default, so calling it won't work. Depending on what you're running this on, you'll want to use either os.execute("sleep " .. number_of_seconds) if you're on a mac, or os.execute("timeout /t " .. number_of_seconds) if you're on a PC. You might want to wrap these up into a function
function my_sleep_mac(number_of_seconds)
os.execute("sleep " .. number_of_seconds)
end
function my_sleep_PC(number_of_seconds)
os.execute("timeout /t " .. number_of_seconds)
end
As for the specific error you're experiencing, I think it's due to your end statement as the end of your program. end in lua doesn't do exactly what you think it does - it doesn't specify the end of the program. Lua can figure out where the program ends just by looking to see if there's any text left in the file. What it can't figure out without you saying it is where various sub-blocks of code end, IE the branches of if statements, functions, etc. For example, suppose you write the code
print("checking x...")
if x == 2 then
print("x is 2")
print("Isn't it awesome that x is 2?")
print("x was checked")
lua has no way of knowing whether or not that last statement, printing the x was checked, is supposed to only happen if x is 2 or always. Consequently, you need to explicitly say when various sections of code end, for which you use end. For a file, though, it's unnecessary and actually causes an error. Here's the if statement with an end introduced
print("checking x...")
if x == 2 then
print("x is 2")
print("isn't it awesome that x is 2?")
end
print("x was checked")
although lua doesn't care, it's a very good idea to indent these sections of code so that you can tell at a glance where it starts and ends:
print("checking x...")
if x == 2 then
print("x is 2")
print("isn't it awesome that x is 2?")
end
print("x was checked")
with regards to your "pastebin" problem, you're going to have to be more specific.
You can implement sleep in OS-independent (but CPU-intensive) way:
local function sleep(seconds)
local t0 = os.clock()
repeat
until os.clock() - t0 >= seconds
end

My code keeps looping

I'm very new to Lua and my code is confusing me, I'm making a maze game just for practise and I've come across an error, everytime I run my code it loops, instead of going to the next part. I will appreciate any help given.
My code:
print ("Welcome to the maze")
input = ""
while input ~= "leave" do
print ("What do you want to do first? Leave or inspect?")
input = io.read()
if input == "inspect" then
print (" You venture towards the maze.")
end
if input == "leave" then
print ("You turn around and run.")
end
end
input = ""
while input ~= "turn around" do
print ("There is a path, which do you want to take, left, right or turn around?")
input = io.read()
if input == "left" then
print (" You turn left to the dark trees.")
end
if input == "right" then
print ("You turn right to the light pathway.")
end
if input == "turn around" then
print ("You turn around and run.")
end
end
Although the logic here is slightly skewed (once you turn around you'll be asked to inspect or leave again), here's how you would get to that second part - it needs to occur if you choose to inspect the maze:
print ("Welcome to the maze")
input = ""
while input ~= "leave" do
print ("What do you want to do first? Leave or inspect?")
input = io.read()
if input == "inspect" then
print (" You venture towards the maze.")
while input ~= "turn around" do
print ("There is a path, which do you want to take, left, right or turn around?")
input = io.read()
if input == "left" then
print (" You turn left to the dark trees.")
end
if input == "right" then
print ("You turn right to the light pathway.")
end
if input == "turn around" then
print ("You turn around and run.")
end
end
end
if input == "leave" then
print ("You turn around and run.")
end
end

Resources