Locked lesson.
About this lesson
How to handle subtraction, multiplication and division flashcards.
Exercise files
Download this lesson’s related exercise files.
Flashcard Subtraction, Multiplication, Division Methods.docx59.2 KB Flashcard Subtraction, Multiplication, Division Methods - Solution.docx
59.8 KB
Quick reference
Flashcard Subtraction, Multiplication, Division Methods
Let's build out the rest of the math methods for our app.
When to use
Do this once you've finished your addition method.
Instructions
You can copy and paste your addition method, just change the math operator for each method:
# Subtraction Method!!
def subtract
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
# Multiplication Method
def multiply
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
# Division Method
def divide
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
- Copy and paste your addition method for each of the other math methods, just change the math operator in each one
- 00:04 So we've got our edition method, let's go ahead and get rid of that.
- 00:08 And let's build out all the rest of the methods.
- 00:10 So I'm just going to copy this.
- 00:12 And I'm going to tap down a bit, paste it in again, and
- 00:16 let's call subtract.
- 00:18 So looking through our code, we've got our same random numbers.
- 00:20 That's fine and correct answer equals random one.
- 00:24 Now we want minus and then we need to change these, correct.
- 00:30 Incorrect, seems to be okay, so now let's call the subtract method and
- 00:36 run this thing and see if it works.
- 00:38 What's one minus 10?
- 00:40 Negative nine, correct.
- 00:41 Now, if we don't want negative numbers, how can we change that?
- 00:45 Well, we could just come up here and with our random numbers,
- 00:48 we can run another if statement that says, if random one is less than random two,
- 00:52 generate a new random number.
- 00:54 Just keep in, put it in a little loop and just keep looping until random one is
- 00:58 larger than random two, I leave that to you if you want to do that.
- 01:02 Kind of an interesting exercise.
- 01:03 So, we've got subtract, let's copy this, or
- 01:07 actually let's add our comment subtraction method.
- 01:12 It's kind of obvious, but I like to get in the habit of commenting things out.
- 01:16 So let's paste again and let's call multiply, right?
- 01:21 So again, we're going to go through here and
- 01:25 change that to multiply, multiply, multiply, and multiply.
- 01:30 And of course, multiplication method.
- 01:36 Okay, so let's go down here and run it, call the multiply method.
- 01:41 You see, as I start to type this in, our program already knows we've got this new
- 01:45 method called multiply and we can just click on it or tab in order to do that.
- 01:48 So let's run this guy.
- 01:49 10 times 6, well, I believe that's 60, correct.
- 01:54 Try it again, 4 times 3, 57, wrong, it's 12, not 57.
- 01:59 Okay, so that's working, we've got one more to go and let's call this divide.
- 02:06 So now we want this and this, and let's see this,
- 02:12 this, you can output this anyway you want.
- 02:17 If you wanted to trade, change this to divided by,
- 02:21 now sort of make it a textual representation.
- 02:24 You can do that to whatever you like.
- 02:25 First, our division method comment, and finally we need to do divide.
- 02:32 Okay, so now if we run this, so 6 divided by 10, this is tricky,
- 02:36 this is kind of hard.
- 02:37 I don't know what 6 divided by 10 is what, point six, hey, look at that.
- 02:42 Run it again, though 0 divided by 10, that's going to be zero, right?
- 02:47 Okay, 7 divided by 5, 1.2, men I'm on fire, but the point is.
- 02:53 We're saying zero is the answer and
- 02:56 we're getting some weird things like it's kind of hard to divide 9 by 10.
- 03:01 So you might want to change this around a little bit to create maybe larger numbers.
- 03:06 You might want to run that if statement to make sure random
- 03:09 one is larger than random two.
- 03:10 And you might want to change these to floats, you can do that.
- 03:13 So, from this let's see, play around with this is a good exercise to
- 03:18 sort of think this through and we're pretty much done with the main
- 03:23 functionality of our program very, very easy.
- 03:26 Nothing crazy here, we generated some random numbers, that was pretty simple.
- 03:29 We have a very simple if/else statement.
- 03:31 And yeah, we've just created four methods.
- 03:34 But think this through like, this is a whole lot of code,
- 03:36 is a whole page of code.
- 03:37 And a lot of this is the same structure, the same code, used over and over again.
- 03:42 How much you do it differently to write the program in less lines of code?
- 03:46 How much you combine some of these things and make more elegant code.
- 03:50 So that's a nice exercise to think through and I definitely recommend you do that.
- 03:54 It's a great way to learn and to sort of expand yourself.
- 03:56 So that's it for this video, and in the next video, we'll go ahead and create our
- 04:00 menu that allows you to pick which one of these you want to do at the beginning and
- 04:04 go from there.
Lesson notes are only available for subscribers.