Locked lesson.
About this lesson
How to handle the addition flashcards.
Exercise files
Download this lesson’s related exercise files.
Flashcard Addition Method.docx59.1 KB Flashcard Addition Method - Solution.docx
59.8 KB
Quick reference
Flashcard Addition Method
Let's start by building an addition method.
When to use
Do this at the beginning of your new flashcard app.
Instructions
So let's build out the addition functionality.
Generate a couple random numbers, prompt the user to add them together, and determine if the answer is correct.
def add
rand_1 = Random.rand(0..10)
rand_2 = Random.rand(0..10)
correct_answer = rand_1 + rand_2
system "clear"
print "What is #{rand_1} + #{rand_2}: "
answer = gets.to_i
if answer == correct_answer
system "clear"
puts "Correct! #{rand_1} + #{rand_2} = #{correct_answer}!!\n\n"
else
system "clear"
puts "Wrong!! #{rand_1} + #{rand_2} = #{correct_answer}, not #{answer}!!\n\n"
end
end
Hints & tips
- Let's build addition flashcards!
Lesson notes are only available for subscribers.