I'm writing an easy conditional structure:
numero = as.integer(readline(prompt="Elige un número cualquiera: "))
if(numero < 0){
paste0("El número", numero, "es negativo.")
} else {
if(0 <= numero <= 100){
paste0("El número", numero, "está entre 0 y 100.")
} else {
paste0("El número", numero, "es mayor que 100.")
}
}
However, when running this code an error arises:
Error in parse(text = x, srcfile = src): <text>:8:18: unexpected '<='
7: } else {
8: if(0 <= numero <= ^
Why is this happening?
CodePudding user response:
Your condition within if()
is not coded properly. In addition, to make a space between strings in a sentence in this case, paste
is better than paste0
.
Here is an alternative fix:
numero = as.integer(readline(prompt="Elige un número cualquiera: "))
if(numero < 0){
paste("El número", numero, "es negativo.")
} else {
if(numero >=0 & numero <= 100){
paste("El número", numero, "está entre 0 y 100.")
} else {
paste("El número", numero, "es mayor que 100.")
}
}
CodePudding user response:
It's not math. You need to be explicit.
Try if(0 <= numero && numero <= 100)
In addition, I would recommend to use the vectorised ifelse()