開始使用免費開始

使用 stop() 函式

使用 stop() 函式用來表示有問題發生。

本練習屬於課程

R 防禦式程式設計

檢視課程

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Stop the execution if ages are negative
mean_age <- function(ages) {
   if(any(ages < ___)) {
    ___("You have negative ages!")
  }
  
  m <- mean(ages)
  return(m)
}
編輯並執行程式碼