0

I can't get the lines that are commented to work. I can use some help here. Also, any recommendations on how to get the updated balance to stay within the function? This way the second time you play the game, you have a different balance than 1,000 depending on how much you bet on the first game. I do not know how to keep the updated balance in a loop for the next time you run the function. Thanks!

playCraps <- function(betSize){
  balance <- 1000
  errors<- character(0)
  if(betSize > balance){
    errors <- paste("Please enter a bet size within your current balance", sep="\n")
  }
  # if(!is.numeric(betSize)){
  #   errors <- paste("Please enter a number", sep="\n")
  # }
  # if(!is.integer(betSize)){
  #   errors <- paste("Please enter a whole number", sep="\n")
  # }
  if(length(errors) > 0){
    stop(errors)
  }
  • You need to use L to explicitly denote an integer. This will return false: `is.integer(3)` This will return true: `is.integer(3L)` – SmokeyShakers Nov 26 '19 at 16:29
  • I'd suggest making balance a parameter of your function. Generally you can't do what you're attempting to do in R, i.e. call and modify a function at once. – SmokeyShakers Nov 26 '19 at 16:32
  • T3HD0NK3Y, you *can* have `balance` persist` between calls (it takes some work), but too often I find that there is an underlying mis-assumption about the design of the function and workflow. Resolving it with persistence may not be the idiomatic, efficient, or resilient/robust solution. Since we don't see your "loop", it's a little difficult to get the full context. Can you add context, sample data, and reproduciblity to the question? (Refs: https://stackoverflow.com/questions/5963269, https://stackoverflow.com/help/mcve, and https://stackoverflow.com/tags/r/info.) – r2evans Nov 26 '19 at 16:44
  • Forget the balance issue for now, I am concerned about getting !is.integer and !is.numeric to work for right now. I do not understand why !is.integer(betSize) does not work as I would like it to. – T3HD0NK3Y Nov 26 '19 at 16:46
  • `is.integer` isn't checking whether a number is an integer by the rules of math. It's checking if it's stored as a an integer. You need to explicitly tell R that you want a number stored as an integer, via `L`. – SmokeyShakers Nov 26 '19 at 18:31
  • Maybe try `if(as.integer(betSize) != betSize)` – SmokeyShakers Nov 26 '19 at 18:39

0 Answers0