I typed this program exactly and it is telling me "invalid syntax"
print("test program for for loop")
a=int(input("enter any no")
for i in range(10):
print(a)
I guess this is what you want to do..
There is syntax error in line 2. You missed closing bracket.
print("test program for for loop")
a = int(input("enter any no"))
for i in range (0,10):
print(a)
Check if you are using python3
Related
Why is it that when using an if else statement in R, the "else" cannot be on a new line unless the whole statement is nested within another form of statement, loop or function?
For example, the below code does not work:
x <- 5
if(x<10){
print("x<10")
}
else{
print("x>9")
}
However if we put the whole thing within a loop so that it is indented, suddenly it works fine
for(i in 1:1){
x <- 5
if(x<10){
print("x<10")
}
else{
print("x>9")
}
}
I understand that R wants you to write them like below:
x <- 5
if(x<10){
print("x<10")
}else{
print("x>9")
}
But I don't like how that looks and also want to understand why this syntax / style is only a requirement when the statement is not nested within something else.
Thanks!
This has nothing to do with enforcing a style, but to do with how the R parser works. In essence, when your if clause is syntactically complete with a newline at the end of its closing bracket, the console thinks you are done with that instruction, and waits for its next instruction. But an else clause has to be part of the same instruction. If the next instruction starts with an else, the R parser doesn't know what the else is referring to.
Let's demonstrate this for clarity. If you paste the following code into the console:
x <- 5
if(x<10){
print("x<10")
}
Then you will see this:
> x <- 5
>
> if(x<10){
+ print("x<10")
+ }
[1] "x<10"
>
Notice that if a line of code is syntactically complete, R will run the code, then print the console marker > at the start of the next line. If a newline symbol is present in the pasted code, but the code is syntactically incomplete, the console places a + at the start of each line, indicating that the R parser needs more input before it has syntactically complete code to run.
But if you run your example code, look what happens:
> if(x<10){
+ print("x<10")
+ }
[1] "x<10"
> else{
Error: unexpected 'else' in "else"
> print("x>9")
[1] "x>9"
> }
Error: unexpected '}' in "}"
Your if statement with its associated brackets was a syntactically complete element, so R ran the code. It printed out [1] "x<10" as expected, then waited for its next instruction. However, its next instruction was else {, and the R parser knows that no syntactically correct R code can start with an else. R can't go back in time and undo what it did in the if clause just because you subsequently write an else clause.
So, this has nothing to do with enforcing a style. You simply need a way of saying to R "I'm not done yet" after an if clause. There are various ways to do this. For example, your code would be fine inside a function, or a loop, or brackets. These are just ways of saying to the R parser : "don't run the code yet, I'm not done."
x <- 5
{
if(x<10){
print("x<10")
}
else{
print("x>9")
}
}
#> [1] "x<10"
Created on 2022-02-11 by the reprex package (v2.0.1)
If I have a for loop with a try-catch block, and I interrupt the kernel, the loop will go to error block and simply proceed to the next iteration. I would like to stop the loop entirely, is there a way to do that? Currently, I'll have to kill the kernel if I want to stop the loop, which means loading up the models, etc again, which takes time.
Example: I would like to know if there's a way to interrupt the entire for loop rather than just one iteration if I made a typo.
import time
for i in range(100):
try:
time.sleep(5)
print(i)
except:
print('err')
just catch your keyboard interrupt in your try/catch.
for i in range(100):
try:
time.sleep(5)
print(i)
except KeyboardInterrupt:
print ('KeyboardInterrupt exception is caught')
raise # if you want everithings to stop now
#break # if you want only to go out of the loop
else:
print('unexpected err')
more info : https://www.delftstack.com/howto/python/keyboard-interrupt-python/
You can break out of the loop:
for i in range(100):
try:
time.sleep(5)
print(i)
except:
print('err')
break
I have a piece of code that I run and I want to execute some code on the exit of a function, e.g. to close a connection.
fn(io) = begin
write(io)
# do lots of stuff which can fail
...
# want close connection
on_exit(()->close(io))
end
For this particular example you would probably use a do block:
open("myfile.txt", "w") do io
write(io, "Hello world!")
end
In the more general case you can use finally. From the docstring:
Run some code when a given block of code exits, regardless of how
it exits. For
example, here is how we can guarantee that an opened file is closed:
f = open("file")
try
operate_on_file(f)
finally
close(f)
end
This is interesting. I'm on CentOS 6.9, R-3.4.2. I have a code, tmp.R:
main<-function(){
a = 9
print(a) xyz
print("Should never get here")
}
When I run this, Rscript tmp.R, I get
Error: unexpected symbol in:
" a = 9
print(a) xyz"
No traceback available
[1] "Should never get here"
Error: unexpected '}' in "}"
No traceback available
This is pretty confusing because I never actually called main(). In fact, if I remove the syntax error
(3rd line becomes print(a)), and I run it, there isn't any output. This is the expected behavior in my mind.
QUESTION : Why does R execute code in a script when a syntax error occurs, even when the code isn't explicitly called(!)?
EDIT : It turns out that this behavior seems to be due to having options(error=traceback) being set in my .Rprofile. This is undesirable behavior none the less. It would still be desirable to be able to get tracebacks when in interactive mode and not do this strange code execution in non-interactive mode.
I want to use readLines function for input from console of variable number of lines and store it to a vector:
v <- readLines()
How do I signal the end of input? Control-c cancels the process and no 'v' object is formed. Control-Z stops R program altogether. Typing 'EOL' or 'EOF' do not work.
I tried following function but it gives error:
getinput = function(){
v=""
while(TRUE){
line = readLines(n=1)
if(line=="") break
v = v+line
}
v
}
> getinput()
firstentry
Error in v + line : non-numeric argument to binary operator
>
I am using R on Debian Linux. Thanks for your help.
<CTRL-D> will signal EOF. If you're using ess, try C-c C-c. Hope that helps and good luck. Leave a comment if you need further assistance.