dimanche 6 décembre 2020

Basic While Loop with If Statement

I'm creating a basic function that runs a while loop and an if statement for an R class and am looking for help.

I don't want to overcomplicate things, so I'd prefer to stick to just the basics with this answer.

I created a basic football score function that's intention is to add 7 points to the score if yards >=80, add 3 points to the total score if yards >= 60 (with else if), and add 0 to the total score if anything <= 60.

This is where I have started:

teamA <- function(drives) {
  i <- 0
  score <- 0
  while (i < drives){
    yards <- sample(0:100,1)
    if (yards >= 80){
      score <- score + 7
    }
    else if (yards >= 60){
      score <- score + 3
    }
    else {
      score <- score
    }
    i <- i + 1
    return (score)
  }
}
teamA(5)

This is obviously not accurate to real football, but I wanted to simplify it for class.

I wanted to make a function where you could specify an amount of drives a team had and compile a score based on a random amount of yards generated by the sample I wrote in the while loop.

Would anyone be able to help fix this code? I'm not very experienced with R and can't think of the best way to solve my issue.

My biggest issue right now is that it seems like I'm only getting one score returned and not compiling a total score.

Aucun commentaire:

Enregistrer un commentaire