multQuiz = function(){ print("You will be given three multiplication problems.") print("You have five tries for each problem.") for (i in 1:3) { a = sample(1:12, 1) b = sample(1:12, 1) answer = a*b guess = 0 tries = 0 while ((guess != answer) & (tries < 5)){ # Convert user input from string to double explicitly; # otherwise R sometimes gives wrong results. guess = as.double(readline(prompt = paste( "What is", a, "times", b, "? "))) tries = tries + 1 if (tries == 5){ print(paste("The answer is", answer, ".")) } } } } multQuiz()