Internet of Chemistry Things: Education | IoCT Education



Python Activity 8: Looping Structures: FOR LoopsLearning ObjectivesStudents will be able to:Content:Explain the difference between while loop and a FOR loopExplain the syntax of a FOR loopExplain how to use the range() function in a FOR loopExplain an accumulator in a FOR loopProcess:Write code that includes FOR loopWrite code that uses use FOR loops within functionsPrior KnowledgePython and Raspberry Pi concepts from all previous activitiesFurther Reading Critical Thinking Questions:Enter the following two Python programs in your Thonny IDE. Make sure your variable window is shown (under View put a check next to Variables). Use the “Debug Current Script” tool under run to “Step into” the program. Press F7 on your keyboard to step through. Be sure to watch the variable values while running these programs.Python Program 1Python Program 2name= input("Enter your name: ")x = 0while(x < 20): print(name) x = x + 1name= input("Enter your name: ")for x in range(20): print(name)a. What is the output for each program?1.a:b. Both programs produce the same output. Which code fragment is more concise?1.b:FYI: The Python predefined range() function is used to define a series of numbers and can be used in a FOR loop to determine the number of times the loop is executed.. 2.Enter and execute the following code fragments in the Thonny editor window and state the output:a.for x in range(5): print(x, end=" ")2.a:b.for x in range(1,5): print(x, end=" ")2.b:c.for x in range(3,20,2): print(x, end=" ")2.c:d.numIterations = 6for x in range(numIterations): print(x, end=" ")2.de.numIterations = 6for x in range(1, numIterations+1): print(x, end=" ")2.e:3.After examining the five code fragments in #2, explain how the range() function works. Include an explanation of the arguments.3.FYI: In a FOR loop you can include a list of values in place of the range() function.4.Enter and execute the following code. for x in [3,6,9,12,15,18]: print(x, end=" ")a.Rewrite this code using the range() function. 4.a:b.Why would you use the range() function when you could just list the numbers?4.b:5.As you learned in Python Activity 7, every loop structure requires three actions. Explain how these actions are implemented in the following Python FOR loop. Use the Thonny debug feature if you need assistance in answering the questions.for x in range(1,5): print(x, end=" ")Initialize a variable used in the test condition;b.Include a test condition that causes the loop to end when the condition is false:c.Within the loop body, update the variable used in the test condition:-10477512636500Information: The type() function indicates the class of data stored in a variable. The str() function converts what is the parentheses ( ) to a String.6.Read through the code and determine what it does. favorite = input("Enter your favorite ice cream flavor: ")for x in range(1,5): print(str(x) + ".", favorite, end="\t")a.Explain what you think the program does.6.a:b.Enter and execute the code to determine if you were correct. What does the program actually do? Provide a detailed explanation.6.b:c.Explain the use of the str() function in the print statement. Why is it needed?6.c:plete the arguments in the following range function so that the code prints the even numbers between 100 and 200 inclusive.for x in range(______________________________): print(x)plete the arguments in the following range function so that the code prints: 5 4 3 2 1 0.for x in range(______________________________): print(x)FYI: An accumulator is a variable that stores the sum of a group of values. 9.Examine the following code segment.total = 0for x in range(5): number = int(input("Enter a number: ")) total += numberprint("The total is:",total)a.Why is the variable total initialized to 0 in the first line of code?9.a:b.Explain what the following code does: number = int(input("Enter a number: "))9,b:c.Explain what the following code does: total += number9.c:d.How many numbers does the program prompt for?9.d:e.What is the accumulator in the code segment?9.e:10. Is it better to use a FOR loop when you know the number of times the loop should be executed or when you do not know? Explain your answer.10.Application Questions: Use the Python Interpreter to check your workWrite a code segment using a FOR loop that prints multiples of 5 from 5 to 500.Individual Homework Activity: Write a program that converts the temperature in Celsius to Fahrenheit. Have the program output two columns. In the first column, it prints out the temperature in Celsius, and the second in Fahrenheit. Have the data print out all integer values of Celsius from 0 to 100 in 3 degree increments. Have the data print out the Fahrenheit to 1 decimal point. Your program must contain documentation lines that include your name, the date, a line that states “PA8 Individual Homework” and a description line that indicates what the program is supposed to do. Take a screen shot of the program and the output and attach the files to homework python activity 8 in Moodle. You can also attach the file that ends in .py to the assignment. (10 points)Sample output (in 4 degree increments for Celsius):Using your Raspberry Pi and Cobbler, build a circuit with a red LED. Assign the LED to GPIO 17. Write a program that uses a FOR loop that starts the light with a value of 0 (completely off) to a value of 1(completely on) in 0.01 increments. Each time the light increases in intensity, the program should wait for 0.1 seconds. Therefore it should take 10 seconds to go from off to full intensity. ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download

To fulfill the demand for quickly locating and searching documents.

It is intelligent file search solution for home and business.

Literature Lottery

Related searches