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!
- 00:04 So I've created a new file called flashcard.rb.
- 00:07 And let's start with addition.
- 00:09 The first thing I want to do is create the addition.
- 00:11 So, first things first.
- 00:12 We need a couple of random numbers.
- 00:14 So let's call them rand_1 and rand_2.
- 00:19 And to do that, we remember, we go, Random.rand, and then give it a range.
- 00:24 We want let's say, let's keep it easy between one and ten, right.
- 00:27 So now we can just copy this, paste it in.
- 00:31 As you know we've got our two random numbers.
- 00:33 Now we need to know what the correct answer is.
- 00:36 So let's create another variable called correct,
- 00:42 correct answer and set that equal to rand 1 plus rand 2.
- 00:48 Right? So we've got our two random numbers.
- 00:50 We know if we add them together, this is what the answer should be.
- 00:54 Right. So let's create some prompts so
- 00:58 print what is and let's do our interpolation,
- 01:04 let's go rand one plus
- 01:08 rand 2 and let's say I need a variable.
- 01:13 Let's call this answer.
- 01:15 It's the person's answer.
- 01:16 And we'll get and
- 01:17 remember we need to change this to an integer because it's a string otherwise.
- 01:22 So now let's just puts the answer to the screen.
- 01:26 Save this and run it.
- 01:27 Remember, I'm a big fan of doing little small chunks of code and
- 01:31 running them often to see if there's any errors.
- 01:33 So 10 + 5, 15, it outputs 15.
- 01:37 So, okay, so far so good seems to be working.
- 01:40 So let's get rid of this.
- 01:41 We don't want to puts the answer.
- 01:42 So now we need to decide whether the answer that user put in is the same as
- 01:47 the correct answer.
- 01:48 Did they get the answer right?
- 01:49 So that sounds like a job for an if statement to me.
- 01:52 So if answer equals, remember the double equal to, correct answer,
- 01:57 let's go puts correct and maybe we want to put a little
- 02:02 message that says let's just copy this, correct.
- 02:07 Random one plus two equals, we can put correct answer.
- 02:14 Now, we want an else statement, else, and
- 02:18 I'm just going to copy this whole line and change Correct to,
- 02:24 Wrong! equals correct answer not your answer!
- 02:30 So let's just save this and run it and see what we have.
- 02:33 So what is 3 + 2, 98.
- 02:35 Wrong, 3 + 2 = 5, not 98.
- 02:38 Try it again.
- 02:39 10 plus 1 is 11.
- 02:41 Correct, one plus 10 equals 11.
- 02:42 So we've got it.
- 02:43 This is it.
- 02:44 This is our functionality for the whole thing, but we like methods,
- 02:48 we should put this in a method.
- 02:50 Let's make this nice and neat.
- 02:51 So instead of just having all this code out in the open,
- 02:55 let's define a method and let's call it add.
- 02:58 And down here at the end let's end our method and
- 03:01 I'm just going to highlight all of this and tab it over.
- 03:03 So let's see now if we save this program and run it,
- 03:07 nothing's going to happen because remember methods don't run unless you call them.
- 03:12 So let's call our add method and see if it worked.
- 03:16 What is 9 + 5, 31, wrong.
- 03:20 Turn it again, 10 + 3 is 10, correct.
- 03:23 So we are good to go.
- 03:24 Now the reason why we want to put this in a method besides the obvious reason that
- 03:28 it just sort of makes sense to chunk all of our code together when we can is
- 03:32 we're going to do a bunch of these.
- 03:33 We're going to do addition, subtraction, multiplication, and division, and
- 03:37 we want to be able to call,
- 03:38 if the user wants to do addition we'll just call the add method.
- 03:41 If they want to do division, we'll just call the division method.
- 03:43 And doing it this way allows us to do that much easier.
- 03:47 Plus it just makes our code easier to read.
- 03:50 And we could even comment this if we want.
- 03:52 Addition method okay, so we're good to go.
- 03:55 So in the next video we will start to work on the other subtraction,
- 03:59 multiplication and division methods and go from there.
Lesson notes are only available for subscribers.