+ 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"?

5th Sep 2021, 7:30 PM
Tony Anciaux
Tony Anciaux - avatar
6 Réponses
+ 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
5th Sep 2021, 8:26 PM
mesarthim
mesarthim - avatar
+ 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!
5th Sep 2021, 7:58 PM
mesarthim
mesarthim - avatar
+ 3
Thank you for your help! Defining as an integer, I should have thought about it. I still need so much practice.
5th Sep 2021, 8:30 PM
Tony Anciaux
Tony Anciaux - avatar
+ 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)
6th Sep 2021, 2:35 AM
Simba
Simba - avatar
+ 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.
6th Sep 2021, 6:15 AM
mesarthim
mesarthim - avatar
+ 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.
5th Sep 2021, 8:19 PM
Tony Anciaux
Tony Anciaux - avatar