I have a sample of dice rolls given by "Rolls<-sample(1:6, 5, replace=TRUE)"
What I want to do is be able to do is ask a user to select two values from the sample, then I want the two values to be added and be stored in the sample as well. (I don't need anything to happen to the values that were used in the addition)
My main issue with this is that I've been using:
"first_number <- readline(prompt="Please enter the first value you'd like to add: ")" (and the same for "second_number")
and this doesn't ensure that the value they choose is within the sample I have.
For example, let's say my sample is 4 5 6 6 2, with the code I'm currently using the user is able to select any value, such as 1, 0, or even 200, even though none of these are in the sample set given. If I could fix this then the rest of what I'm trying to do will be simple.
Any support would be much appreciated
Perhaps like this:
#Rolls <- c( 4,5,6,6,2 )
set.seed(100)
Rolls<-sample(1:6, 5, replace=TRUE)
chosen <- c()
ordinals <- c("first","second")
while( length(chosen) < 2 ) {
cat( "Dice = ",Rolls, "\n" )
pick <- readline(
prompt=paste("Please enter the", ordinals[length(chosen)+1],"value you'd like to add: ")
)
if( pick %in% Rolls ) {
chosen <- append( chosen, as.numeric(pick) )
Rolls <- Rolls[ -match( pick, Rolls ) ]
} else {
cat("Whoops, that's not a valid choice!\n")
}
}
Looks like this in my session:
Dice = 2 6 3 1 2
Please enter the first value you'd like to add: 4
Whoops, that's not a valid choice!
Dice = 2 6 3 1 2
Please enter the first value you'd like to add: 2
Dice = 6 3 1 2
Please enter the second value you'd like to add: 5
Whoops, that's not a valid choice!
Dice = 6 3 1 2
Please enter the second value you'd like to add: 3