我已经习惯了Python,在Python中,以下代码可以毫无例外地工作。然而,当我尝试运行该命令时,我在R中得到以下错误。
a <- readline(prompt="Give a num between 1-10: ")
b <- readline(prompt="Give another between 11-20: ")
if((1 <= a <= 10) & (11 <= b <= 20)) {
a <- a
b <- b
} else {
a <- readline(prompt="Give a correct num between 1-10: ")
b <- readline(prompt="Give a correct num between 11-20: ")
}错误:

发布于 2020-05-23 10:07:27
1)尽管1 <= a <= 10在逻辑上是正确的,但它在R中不是有效的语法。您需要单独使用1 >= a & a <= 10。
2) readline的输出是一个字符,您可能需要对as.numeric进行换行以获得一个数字。
3)不需要重新赋值a和b,如果条件为TRUE,它已经存在。
所以总结一下你可以做的事情:
a <- as.numeric(readline(prompt="Give a num between 1-10: "))
b <- as.numeric(readline(prompt="Give another between 11-20: "))
if(a < 1 | a > 10)
a <- as.numeric(readline(prompt="Give a correct num between 1-10: "))
if(b < 11 | b > 20))
b <- as.numeric(readline(prompt="Give a correct num between 11-20: "))https://stackoverflow.com/questions/61966524
复制相似问题