Download presentation
Presentation is loading. Please wait.
Published byBennett Eaton Modified over 8 years ago
1
Keywords: Range Trace table Testing List Index Activities: To create a trace table for a small piece of code using a set of data. Create a trace table for a given scenario and set of data. Write the Python code for the scenario. Test the program using trace table data. Lesson Objective: Use range in a loop. Introduce a simple number list. Use trace tables effectively to test output.
2
Syntax: allword=“December” partofword=allword[0:3] Slicing example: partofword=allword[0: 3] Starter : Slicing start end (end number not included).
3
Syntax: range(start,end) Loop example: for loopcounter in range(0, 10): print (loopcounter) Looping for a set number of times start end (end number not included).
4
Syntax: mylist=[1,2,3] firstitem=mylist[0] Loop example: mylist=[1,2,3,4,5] for loopcounter in range(0, 5): print (mylist[loopcounter]) Simple List end (end number not included). 0 4
5
Code Example Input: 2 3 7 9 numberlist = [2,3,4,5] yournumber=0 numberfromlist=0 yoursum=0 for loopcounter in range(0,4): yournumber = int(input("Input a number: ")) numberfromlist=numberlist[loopcounter] yoursum=numberfromlist+yournumber print("Answer is : " + str(yoursum))
6
numberlist = [2,3,4,5] yournumber=0 numberfromlist=0 yoursum=0 for loopcounter in range(0,4): yournumber = int(input("Input a number: ")) numberfromlist=numberlist[loopcounter] yoursum=numberfromlist+yournumber print("Answer is : " + str(yoursum)) Code Example Trace Table VariableStart Value numberlist [2,3,4,5] loopcounter0 yournumber 0 numberfromlist 0 yoursum 0
7
for loopcounter in range(0,4): yournumber = int(input("Input a number: ")) numberfromlist=numberlist[loopcounter] mysum=numberfromlist+yournumber print("Answer is : " + str(mysum)) Trace Table Input: 2 3 7 9 numberlist loopcounter yournumbernumberfromlist yoursumoutput [2,3,4,5]0000 0 1 2 3 2 2 Answer is: 4 4 3 36 Answer is: 6 7 9 4 5 11 14 Answer is: 11 Answer is: 14 Input a number: 2 Answer is : 4 Input a number: 3 Answer is : 6 Input a number: 7 Answer is : 11 Input a number: 9 Answer is : 14
8
Activity 1 Input: 2 4 6 8 loopcounter yournumber yoursumoutput 0 0 1 2 3 Write a program: 1.Use For Loop and Range to loop 4 times. 2.Write code to enter a number. 3.Write a line of code to calculate the number you have entered times (x) 5. 4.Print result. 5.Create trace table. 6.Run code, input set numbers and compare output with your trace table.
9
Activity 2 Write a program: 1.Create a list with 4 numbers. 2.Use For Loop and Range to loop 4 times. 3.Write a line of code to times (x) next number in the list by 10. 4.Print the result. 5.Create a trace table. 6.Run code and compare output with your trace table. numberlist loopcounter numberfromlist yoursumoutput 000 0 1 2 3
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.