DS4PS / cpp-527-fall-2020

http://ds4ps.org/cpp-527-fall-2020/
0 stars 1 forks source link

Lab 03 #3

Open lecy opened 4 years ago

lecy commented 4 years ago

QUESTION FROM STUDENT:

This is my code for question 3 , however I am not able to run game=="goat" like you did in the lecture. I am getting object game not found and I can seem to know why .

open_goat_door <- function( game, a.pick ) 
{

# Scenario1
# game <- c("goat","car","goat")
# a.pick <- 1

game
a.pick
doors <- c(1,2,3)

possible.doors <- doors[ game=="goat" & doors!=a.pick ]
opened.door <- sample( possible.doors, size=1 )
return( opened.door )

}
lecy commented 4 years ago

Note that when you are in development mode for functions in order for the code to run you need to assign actual values to the arguments.

# Scenario1
# game <- c("goat","car","goat")
# a.pick <- 1

When something is commented out you can run it by highlighting the code you wish to run (omit the hash tags) and type control+enter in R Studio. This runs only the syntax that is highlighted, not the entire chunk.

These lines are commented out so that when you run the full function they are ignored. Only when testing the function during development would you need to instantiate these variables.


You can see that in the test code as well, we run a couple of functions before we run the function that we are testing. This is so that we can generate the game info needed for the specific function we are testing:

# set game up
this.game <- create_game()
# contestant selects first door
my.initial.pick <- select_door()

# TEST OPEN GOAT DOOR FUNCTION
open_goat_door( game=this.game, a.pick=my.initial.pick )

Note the first argument: game=this.game

this.game contains the actual information about the goats and car that was generated in the first function.

The argument game is an empty object that will store values (the play game vector) once they are assigned.

Similar to a problem in algebra:

x = 7
y = 2 * x
# solve for y

When creating functions the argument name (e.g. game) is just a placeholder like X is just a placeholder above. It will not take on a value until one is assigned (game=this.game), similar to the line x=7 above.

malmufre commented 4 years ago

Thank you Dr Lecy,

I have tried removing the hashtags as well , but it still gave me that the object game is not found.

 a.pick<-1

game
a.pick
doors<-c(1,2,3)

possible.doors<- doors[ game=="goat" & doors!=a.pick]
opened.door<-sample(possible.doors,size=1)
  return(opened.door)

Error: object game not found and the arguments inside the object game keep on changing every time I run the code which makes the doors 1, 2 ,3 not represent the arguments in the object game. Is that Okay?

lecy commented 4 years ago

You still need to assign a value to the objects that take the place of your arguments:

game <- c("goat","car","goat")
a.pick <- 1
malmufre commented 4 years ago

I had the game<- c("goat","car","goat") but It wouldn't work for some reason . Now it did. though.

Thanks!