How to use SNAP To Create a Coin

  • Slides: 28
Download presentation
How to use SNAP! To Create a Coin Toss Program

How to use SNAP! To Create a Coin Toss Program

What is SNAP! Snap! Is a computer programming language created to help students create

What is SNAP! Snap! Is a computer programming language created to help students create interactive animations, games, stories, and more, while learning about mathematical and computational ideas. Snap! was inspired by Scratch, but also targets both novice and more advanced students by including and expanding Scratch's features. It is entirely browser-based, with no software that needs to be installed on the local device.

How to Begin You will want to go to the link here to see

How to Begin You will want to go to the link here to see a possible implementation of what you are trying to achieve with the simulation.

Now What? Before starting to program in Snap!, plan out the sequence of events

Now What? Before starting to program in Snap!, plan out the sequence of events that will occur in your simulation:

What will you need to do Have a way to start the program. .

What will you need to do Have a way to start the program. . Have some variables named to do stuff with. A variable is the something that can change in a program. Have the computer generate a 1 for heads and a 2 for tails Have the computer change the way the picture, called a sprite, looks if you get heads or tails generated Keep count of the number of heads and tails it flips and total flips Calculate the Probability of tails and heads from the information Flip a coin based on how many flips the user wants to do. Make a noise when done.

What will you need to do first! 1. Go to this site. https: //snap.

What will you need to do first! 1. Go to this site. https: //snap. berkeley. edu/snapsource/snap. html# 2. Click the cloud. Sign in (sign up) 3. This program has certain things already made in the program. A noise, a coin head, ta coin tail. You can find these under the icons sounds and costumes. For now click on scripts. This is where we will add blocks of code to make a program.

What will you need to do second You have to have a way to

What will you need to do second You have to have a way to start the program. We will use the following. This is the first block you will put in on the script area. Notice the top does not have a place to connect things at, but the bottom does.

Name Your Variables On the far right are your block storage areas. They look

Name Your Variables On the far right are your block storage areas. They look like this. Click the set that says variables. All the blocks that show up are light brown.

Name Your Variables We will add a variable for each of these items. The

Name Your Variables We will add a variable for each of these items. The arrow can be clicked on or off. The arrow simply means if it shows up on the stage with you character or not.

Set Variables To 0 You will want to start your program with everything set

Set Variables To 0 You will want to start your program with everything set to 0 each time you start it. There is a block under the variables block set you will use/ It looks like this: You will make one copy for each variable we named. When you put them on your script area, you can change the drop down arrow to the name of each your variables. Here is an example.

Now We Need Some Data We want the program to get the number of

Now We Need Some Data We want the program to get the number of flips it should make from the user. So we will go to another set of blocks called the sensing blocks. All these blocks are blue. We want to ask a question, so choose the block and drag it to the script area.

How To Add The Question Answer To The Program. So now we need to

How To Add The Question Answer To The Program. So now we need to let the computer know that this is the answer to one of our variables we made. Go back to the variable block set ( light brown) and find another “set to” block like this one. Click the arrow and choose the variable we made How Many Coin Tosses. We will change the 0 to reflect the answer the user put in. To do this, go to the sensing set and find the block answer. Drag it to where the 0 is. The block should look like this.

Your Doing Great! Let's Talk About Random # Generators A random # generator is

Your Doing Great! Let's Talk About Random # Generators A random # generator is a piece of code that will randomly choose a number between two numbers. Its already made for you. In the case of a coin flipper program , we want to have the computer randomly choose a “ 1” to mean heads and a “ 2” to mean tails. We are going to have one of our variables be a random# generator. First go to the variables blocks Then choose the set to block and put it on the script page. Now go to the operators blocks. They are green.

Let's Use a Random # Generator Find the pick random block. It looks like

Let's Use a Random # Generator Find the pick random block. It looks like this. Put it on the script. Change the numbers to 1 and 2. Then put the operator in your set to block, and change it to the variable name “Heads or Tails Outcome”

What Do I Do Next? Time to set some conditions with loops/ conditionals We

What Do I Do Next? Time to set some conditions with loops/ conditionals We want our program to flip the coin based on how many times we told it to earlier. So we will need a special block For i=. This is not in your set of commands yet, so we will need to pull it from the library. Go to the icon at the top that looks like a sheet. Click libraries. A large screen of choices will show up.

Click Iteration, then scroll down till you see the for i= 1 Step 1

Click Iteration, then scroll down till you see the for i= 1 Step 1 to 10. If it has moved, just search for it. When you find it, you will click import.

For i= 1 to 10 Loops This is a conditional loop. The for i=1

For i= 1 to 10 Loops This is a conditional loop. The for i=1 part says to do the stuff inside the bracket, but each time you do the stuff inside, you will add 1 to a counter. Once the counter reaches the end, the last step, go on to the rest of the program. We will tell our block to loop until the counter reaches the number of tosses they inputted earlier. We do this by putting another answer block where 10 was.

Where are we at?

Where are we at?

Put Your Random Generator Into The Loop Now add in the random generator we

Put Your Random Generator Into The Loop Now add in the random generator we made earlier. It will be the first block in the loop we made. Now our program will generate a number 1 or 2.

If/ Else Statement We need to gather data depending on if it generates a

If/ Else Statement We need to gather data depending on if it generates a 1 for heads or a 2 for tails. We will use a new block called an if/ else statement block. It looks like this under the control set of blocks. Get one and put it on your script page.

Set Up If/ Else Statement Get an operator from the operator block set that

Set Up If/ Else Statement Get an operator from the operator block set that looks like this And put it into the if/ else with the variable from the variable blocks that says “ Heads or Tails Outcomes”. It should look like this: Now the program knows that if it generates a 1, it should go into the block and do a command, else, it skips that area of the program.

Fill In The If/ Else Statement If a 1 is generated, you want the

Fill In The If/ Else Statement If a 1 is generated, you want the head costume to show for second and you want the variable for Heads to add 1 to its count. If a 2 is generated, you want the tail costume to show for a second and you want the variable for Tails to add 1 to its count. This is how the If/ Else should look when done.

Probability Remember that we still want the program to determine the probability of the

Probability Remember that we still want the program to determine the probability of the heads and tails. It should add up to 100% to do that, we will use the following blocks. Go to the variables block set and find the “change __ by 1” block. It looks like this: You will need 2. Click the drop down arrow. One should say Probability of Heads and the other Probability of Tails.

Now go to the operators block set and find one that looks like this.

Now go to the operators block set and find one that looks like this. You will need 2 total. Fill the blanks in with the variables under the variable block set for heads / how many coin tosses and tails/ how many coin tosses, like this: Now put them into the “ change ___ by 1 blocks you pulled out earlier they should look like this:

Add Sound Add your sound Go to the sound block set and find the

Add Sound Add your sound Go to the sound block set and find the block “play sound”: Click the drop down arrow to pop.

Final program

Final program

Output On The Stage Run by clicking the flag. Click the coin to start.

Output On The Stage Run by clicking the flag. Click the coin to start.

Good Job!!!

Good Job!!!