Take a minute to explain this syntax aloud to yourself or a colleague before reading the discussion below. How does the syntax of the print(“Player ”+str(player)+“ guess?”) line of code work? What is each part of this statement doing? What numbers do you expect to see most often for c? Why? What do they represent?.What do you think the maximum value will be? What does it represent in this context?.What do you think the minimum value of c will be? What does this represent in this context?.This is a great place to make some predictions: Then, what gets printed? The value of one of the numbers? No, c, which represents how many numbers we needed to randomly calculate before we got two in a row that were the same. The program will continue to run through this loop of finding a new random number ( n), adding 1 to the count variable to keep track of how many numbers we’ve found, comparing the new number ( n) to the previous number ( t), and then exiting the loop if these values are the same or storing the new number to the previous number variable and continuing the process. (Since the conditional for this While loop is True, it will keep running until it encounters a break command.) So, what happens after the else command executes and the value of n gets saved in t? Well, we’re at the end of our loop, so the loop will circle back to its beginning. Why? So, that we can go on to find another, new number (our third number), store this to n, and then compare it to the number we found most recently (now stored in t). Now, t no longer represents the first number we found - it represents the second number we found. In this case, the value that was stored in n (in other words, the most recent random number we found) will be stored into variable t. If t and n don’t have the same value ( what are the chances?), then the program will skip down to the criteria in the else command. If this happens, the command within the If block will run - specifically, the break command will make the program exit the While loop and move on to the print command. If they are the same, that means that the program generated two consecutive numbers that are the same. Why? The program is checking to see if n (the new number) is the same as t (the previous number). Next, the program checks to see if t and n have the same value. Now c has a value of 2, representing that we have found 2 numbers so far. Also, the value of c is increased by one to show that a new number has been randomly determined. Within the loop, a new variable n is created and it’s assigned a random number from 1–6. Next, a Do While loop (modeled using a While loop with a condition of True) kicks in. c will be used as a count variable in this program to keep track of how many numbers are generated before two consecutive values are the same. This program starts by assigning a random number from 1–6 to variable t and storing the value of 1 to variable c. Explain it aloud to yourself or a colleague, then read through a sample description below. How does the do_while_loop.py program at the bottom of page 1 work? This program may not be intuitive. Tech tip: The break command can be typed in via the TI-Nspire™ keyboard. TI-Nspire™ keyboard or from Menu > Random. Tech tip: randint() can be entered via the So, randint(1,6) will return a random integer from the set. Quick refresher: How does the randint(1,6) syntax work?Īnswer: In Python, randint(a,b) returns a random integer within the inclusive range from a to b (meaning including a and b). Start by working through that activity, then read through the following discussion of main ideas from the activity. A great introduction to this loop structure can be found in Unit 4, Activity 3.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |