Homework6

pdf

School

San Jose State University *

*We aren’t endorsed by this school

Course

30

Subject

Computer Science

Date

Feb 20, 2024

Type

pdf

Pages

4

Uploaded by MinisterBravery13388

Report
06 HW - Iteration Problem 6.1 - Breaking out of a Repetition Loop Try the following code snippet in a Python code cell. Try changing various constants and report on what happens in a Markdown cell. 1 2 3 4 5 i is 5; We're 'outta here! 3 5 i is 5; We're 'outta here! In the code above, the starting value of i was set at 3 and it was incremented by 2 (i = i+2) The code gave the value of i until it reached i == 5 because of the if conditional statement which was set to break when i == 5. I can also notice that we recieved less values as compared to the original code because of the changes I made in this new code. Problem 6.2 - Skipping Some Code in a Loop Sometimes you might want to skip executing a portion of the code block being repeated when some condition occurs, but not completely terminate the loop. This can be done using the continue keyword. Consider the snippet of code below. Try this snippet in a Python code cell. Report on what happens in a markdown cell. In [1]: i = 1 while ( i <= 10 ): print ( i ) if ( i == 5 ): print ( "i is 5; We're 'outta here!" ) break i = i + 1 In [4]: i = 3 while ( i <= 10 ): print ( i ) if ( i == 5 ): print ( "i is 5; We're 'outta here!" ) break i = i + 2 In [6]: i = 1 while ( i <= 10 ): if ( i > 4 and i < 8 ): i += 1 continue
1 2 3 4 8 9 10 In this code above, the value of i is set at 1 which is the starting point. There is also an if conditional statement that checks if is greater than 4 and less than 8, which it skips since it doesn't satisfy the condition. So it skips to print(i) statement skipping the range from (4,8) and increases by an increment of 1(i = i+1) Problem 6.3 - The Countdown Modify this for loop: for i in range ( 10 ): print ( i ) to print sequentially backwards from 10 to 1, and then print "Blast off!" 10 9 8 7 6 5 4 3 2 1 Blast off! Problem 6.4 - Using Nested Loops, Part 1 In a new code cell, modify the code from the lecture/lab, but make it so it will print the asterisk pattern like this: 54321 asterisk pattern ***** **** *** ** * ***** **** *** print ( i ) i = i + 1 In [15]: i = 1 for i in range ( 10 , 0 , - 1 ): print ( i ) if ( i == 1 ): print ( "Blast off!" ) In [75]: for i in range ( 0 , 6 ): for j in range ( 0 , 5 - i ): print ( '*' , end = '' ) print ()
** * Problem 6.5 - Using Nested Loops, Part 2 In a new code cell, use nested for loops to print the asterisk pattern, so it looks like this: 12345 asterisk pattern * ** *** **** ***** * ** *** **** ***** Problem 6.6 - Approximate Pi Write a program which will ask a user for the precision required and will then approximate Pi to the requested precision using the Gregory series: Hint : Look up how we estimated Euler’s number e during the lecture 3.1416426510898874 Problem 6.7 - CPX Write a program that varies the tone played by the speaker based on light intensity. Refresh the tone every 0.5 seconds. The program should run for 10 seconds each time you run it, after which it quits. You will have to experiment a little to calibrate the program, so it plays a tone in the light conditions that you are operating in. In [7]: for i in range ( 1 , 6 ): for j in range ( 0 , 5 - i ): print ( " " , end = "" ) for k in range ( 1 , i + 1 ): print ( "*" , end = "" ) print () In [6]: import math pi = 0 k = 1 epsilon = 1000 Targeted_precision = float ( input ( "Enter your targeted precision" )) while ( abs ( epsilon ) > Targeted_precision ): prev_pi = pi pi += ( 4 * ((( - 1 ) ** ( k + 1 )) / (( 2 * k ) - 1 ))) k += 1 epsilon = abs ( pi - prev_pi ) print ( pi )
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Hint: The time.time() method (in the time library) returns the number of seconds elapsed since the start of this epoch. Given above is the code for Problem # 6.7 The frequency has been increased by 6 times as the audio was not very audible previously Submission Submit a .ipynb and .pdf file through Canvas. It should include the code you wrote in Mu for Problem 6.7 and a 15 second video (embedded in your .ipynb file) of you demonstrating the code on your CPX. In [ ]: import time from adafruit_circuitplayground import cp Initial_time = time . time () while ( time . time () < 10 + Initial_time ): cp . play_tone ( cp . light * 6 , 0.5 )