Locked lesson.
About this lesson
Allow the user to choose which type of flashcard game to play with a menu.
Exercise files
Download this lesson’s related exercise files.
Creating a Menu.docx60.5 KB Creating a Menu - Solution.docx
59.8 KB
Quick reference
Creating a Menu
Let's build a menu!
When to use
This technique will work whenver you need to create any sort of menu.
Instructions
First we need a menu to output a menu with options that people can choose:
def menu
menu_string = "1. Addition\n"
menu_string += "2. Subtraction\n"
menu_string += "3. Mulitplication\n"
menu_string += "4. Division\n"
menu_string += "5. End Game\n"
menu_string += "What Flashcard Would You Like? (1-4): "
return menu_string
end
Next we need a loop that will take action based on which of the menu items gets selected:
menu_choice = 0
while menu_choice < 6 do
puts menu
menu_choice = gets.to_i
if menu_choice == 1
add
elsif menu_choice == 2
subtract
elsif menu_choice == 3
multiply
elsif menu_choice == 4
divide
else
menu_choice == 5
exit
end
end
Hints & tips
- A while loop works great for creating a menu
- Put your menu options in a method
- 00:04 Now it's time to build our menu and that seems like a job for methods.
- 00:08 So I'm going to come up here and define let's call it menu and end it.
- 00:12 And inside of here, I'm just going to paste this bit of code and
- 00:15 we can take a look at it.
- 00:17 So if you might remember,
- 00:17 way back when I said it's not a great idea to puts out of a method.
- 00:21 Instead, you want to return.
- 00:23 So here, what we're doing is, we're building a string and
- 00:25 we're going to call it menu string, and then at the end we're going to return it.
- 00:28 And what we're doing here is,
- 00:30 plus equaling all of this stuff into one big string.
- 00:34 So you see on the first line it's 1 addition, and we see this slash and
- 00:37 that's a carriage return.
- 00:39 And if you think back to puts, puts puts things on separate lines and
- 00:43 it does that by adding a carriage return.
- 00:45 Remember chomp, chomp removes a carriage return.
- 00:48 So this is an actual carriage return.
- 00:50 This is what it looks like if you type it out, \n.
- 00:52 So that'll allow this, each of these items to be on
- 00:55 their separate line even though they are all in one string.
- 00:58 So we have subtraction, multiplication, division.
- 01:00 Number 5 End Game, this will stop the game.
- 01:03 And then, here at the bottom we have just a little prompt that says, pick which
- 01:06 flash card would like, 1 through 4, and a little space, and then we just return it.
- 01:10 So if we come down here to the end and we just call out our menu,
- 01:14 obviously nothing will happen.
- 01:15 But if we puts that to the screen, save it and run this thing,
- 01:19 we get this nice looking little menu.
- 01:21 And now obviously we can't do anything, the program ends after this runs.
- 01:24 Or we can take a look and see that, it looks pretty good.
- 01:26 So now, the last thing to do is to gets our user input, so
- 01:30 somebody can actually pick one of these and it will do something.
- 01:34 And for that we're going to use a loop.
- 01:35 So I'm going to use a while loop, you can use any kind of loop, but
- 01:38 I like while loops, so that's what I'm going to use.
- 01:40 And, so I'm going to create a counter.
- 01:41 I'm going to call it menu choice, and I'm going to set it equal to 0.
- 01:45 And then we can go while menu choice is less than 6,
- 01:51 do something and end.
- 01:54 And the reason why I picked 6 is, there are 5 menu items.
- 01:59 So we're going to pick between 1 through 5.
- 02:01 If we pick 6, the whole thing will end, because there isn't a 6, right?
- 02:05 Or if somebody picks 127, that's greater than 6 and it will end.
- 02:08 And so, it's a good way of sort of do that.
- 02:10 So next, we just want to puts the menu to the screen and
- 02:14 then we can get rid of this.
- 02:16 Now we need to grab the user input.
- 02:18 We need to gets it.
- 02:19 So we go menu underscore choice equal gets, and then we need
- 02:25 to make sure to change this to an integer because we're dealing with numbers here.
- 02:28 Okay, so now we're good to go.
- 02:28 Now, the only thing we need to do is run a conditional statement
- 02:32 to decide what to do based on what a person types in.
- 02:35 If they type in number 1, we want to do addition, if they type in 2, subtraction,
- 02:40 multiplication, etc.
- 02:41 All the way down to number five.
- 02:42 So I'm going to go, if menu underscore choice equals 1 do something.
- 02:49 And we'll fill this in in a bit.
- 02:50 Next, we'll go elseif menu, choice equals 2, do something.
- 02:56 I'm just going to copy and paste, 3, 4, 5.
- 02:59 So 1, 2, just change these to 3, 4, 5.
- 03:04 Okay, so what do we want to do inside of here?
- 03:07 Well, all we have to do is call our various functions, right?
- 03:11 So 1 is addition, so that's add.
- 03:14 2 is subtraction, so that's subtract.
- 03:18 And these are just the method names.
- 03:19 3 is multiplication, so multiply.
- 03:22 4 is division, so divide.
- 03:25 And 5, we don't need an elseif,
- 03:27 we can just do else because it's the last thing and then end our if statement.
- 03:32 And we also want the program to end if they pick number 5,
- 03:35 because number 5 was end the game, right?
- 03:38 And so, a handy way to do that is just to type exit.
- 03:42 So if we run this, and go number 1, what is 6 plus 8, 1, wrong!
- 03:47 We get the menu again, what flashcard do you like?
- 03:49 Let's try subtraction.
- 03:50 What is 1 minus 10, negative 9, correct!
- 03:53 But you see, as we're doing this, it's getting really cluttered and
- 03:57 really kind of hard to read, so if we click 5 to end.
- 03:59 And let's clear the screen.
- 04:01 What I'm going to do is, I'm going to come up here and
- 04:03 will grab this system clear, I'm just going to copy it.
- 04:05 And I'm going to come over to our addition method, our add method.
- 04:09 I'm going to paste it here, and
- 04:11 then I'm going to paste it in the if statements as well.
- 04:14 And I'm going to do the same thing for subtraction
- 04:19 there, and multiplication, and you can go through here and do this.
- 04:25 All viewers.
- 04:27 That's good enough, we'll just save that.
- 04:30 So now if we run this again, you see let's try addition, boom,
- 04:33 it clears the screen right over the top.
- 04:35 Nice and easy to read.
- 04:36 4 plus 8 is 12, correct.
- 04:37 And the menu pops up right underneath it, we get a little space there.
- 04:41 Let's try multiplication.
- 04:43 5 times 1, 55, wrong!
- 04:46 Let's try subtraction.
- 04:47 10 minus 3, 7, correct!
- 04:51 And then 5, lets go end game, boom, it ends.
- 04:54 So, that's it and this is a really interesting way to make a menu.
- 04:58 And take a few minutes to look through this again.
- 05:01 This probably makes sense, this is really straightforward.
- 05:04 We looked at elseif statements, a whole bunch in this course.
- 05:07 But take a look at this menu,
- 05:08 and how we strung this together with carriage returns and stuff.
- 05:11 It's kind of a neat way to do menus if you ever have to do menus.
- 05:14 So, we are done.
- 05:16 That is the end of the course.
- 05:17 I hope you enjoyed yourself, I hope you learned a whole lot.
- 05:18 My name is John Elder and thanks for watching.
Lesson notes are only available for subscribers.