I have to create a function in R where the program picks a number between 1 and 100 and asks the user to guess. If it is too low it returns "too low" if its too high I return "too high" if after 7 guesses the user is still wrong I stop the function.
I made the function but cannot find a way to stop it after 7 times!! I want to place a for loop but dont know where can anyone help me?
guess <- function(g) {
ran <- sample(1:100, 1)
if (g < ran) {
print("Too low")
m <- readline("Type number again:")
num <- as.numeric(m)
} else if (g > ran) {
print("Too high")
m <- readline("Type number again:")
num <- as.numeric(m)
} else if (g == ran) {
print("Correct")
}
}