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.
Syntax: allword=“December” partofword=allword[0:3] Slicing example: partofword=allword[0: 3] Starter : Slicing start end (end number not included).
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).
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
Code Example Input: 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))
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
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: numberlist loopcounter yournumbernumberfromlist yoursumoutput [2,3,4,5] Answer is: Answer is: 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
Activity 1 Input: loopcounter yournumber yoursumoutput 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.
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 Print the result. 5.Create a trace table. 6.Run code and compare output with your trace table. numberlist loopcounter numberfromlist yoursumoutput