Solution: Addition Quiz Game
Explore how to create an addition quiz game in Ruby by generating random numbers and managing user input through the command line. Understand best practices for incrementing counters and validating answers to develop interactive command-line applications.
We'll cover the following...
We'll cover the following...
Solution
count = 0
10.times do
num1 = rand(10)
num2 = rand(10)
print "#{num1} + #{num2} ="
answer = num1 + num2
input = gets.chomp.to_i
if answer == input
puts "Correct!"
count = count += 1
else
puts "Wrong!"
end
end
puts "Your score is #{count}/10"
Solution for addition quiz game
Explanation
Lines 3–4: We generate two random numbers in the range of 0–9 by using
rand(10)...