+ 2
Why do I get a "Unexpected Input" error in R?
x <- readLines('stdin') x <- as.integer(x[1]) #define the function stars(x) <- function(x) { result <- ("*" * x) print(result) } The exercise requires to output the number of "*" corresponding to user input in R. I've tried without brackets, I've tried with "\", I've even tried without quotes... The error is always "unexpected input"?
6 ответов
+ 3
I found the problem here. Suppose firstly, you should the stars define as a string then it's shown.
Check my code.
https://code.sololearn.com/c51L1zfgH3Hi/?ref=app
+ 3
Your code is wrong, if you want to write the stars as much as input you should use a loop. Try this one,
x <- readLines('stdin')
x <- as.integer(x[1])
#define the function
stars <- function (x){
for (i in 1:x){
print("*")
}
}
stars(x)
Good luck in coding!
+ 3
Thank you for your help! Defining as an integer, I should have thought about it.
I still need so much practice.
+ 3
It's not necessary to initialize an output variable. Maybe her previous solution included invalid characters.
x <- readLines('stdin')
x <- as.integer(x[1])
stars <- function(x) {
for(i in 1:x){
print("*")
}
}
stars(x)
+ 3
You're right Simba, thanks. I suppose the problem was just this statement "#define the function ". While I was writing this statement, I didn't realise. The part "the function" not a comment part. So, there was a problem.
+ 1
Still gives me the same error message:
Error: unexpected input in " "
Execution halted
It seems it doesn't like the * in the quote marks. The \ does nothing to it. I really don't understand the point.