# ISY104 Repetition Control Structure: Activity Sheet Week 4 Loops Assessment Answer ISY104 – Foundations of Programming Week 4 – Loops

Objective:

This week we will work on repetition control structure

During the workshop we will discuss the answers to question 1 on pages 1 and 2 and then we work on solving the questions in the latter part of page 2.

1. What is the output of each of the following segments of code. Your tutor will discuss this with you.

 Code Segments Answer Reasoning 1 while True: print(“hello”) helloon infinite number of lines. The program doesnot terminate Does not terminate because the while loop condition never becomes False 2 x =3 while True:x= x-1if x == 0: breakprint(x) 21 The loop terminates when x is 0 even though it has a while True because of the break statement within the loop 3 x = 3 while x>0:print(x) x = x -1 321 The while loop terminates when is x is 0. 4 #assume user enters -3 first time and 5 the second time during an executionx=-1while True:x = int(input("Enter a positive number\n"))if x >=0:breakprint(x) 5 If the user enters -3 the if statement will be False, so the break statement is not executed. This causes the loop to execute again. Second time around, the loop will terminate since the user enters 5 5 #Assume user enters 3, 1 and 7 when the code executessecretNos=[2, 4, 7]#01while True:#02guess = int(input("Guess a secret number between 1 and 7\n")) #03if guess in secretNos:#04 print("Correct guess")#05break#06else:print("Incorrect guess")#07 Incorrect guess Incorrect guess Correct guess [2, 4, 7] is a list data type in Python with values 2, 4, 7.The if statement on line 04 uses the in operator to check ifvalue in guess exists in the list. It breaks the loop if it exists. 6 for aVal in range(3): print(aVal) 012 The range() function in Python creates a sequence of numbers from

 7 for aVal in range(3, 0, -1): print(aVal) 321 The range() function in Python creates a sequence of numbers from3 to 1. The for loop is used to iterate through each value in the sequence. 8 for aVal in range(3, 1, -1): print(aVal) 32 Similar reasoning 9 for aVal in range(5, 7, 1): print(aVal) 56 Similar reasoning 10 f=1for i in range(1,4,1): f= f*iprint(f) 6 The factorial of 3 is got. 11 s=0for i in range(1,5,1):s= s+i print(s) 10 The sum of the first 4 numbers. That is 1+2+3+4

You will find solutions to the following with the help of your tutor.

Activity – 1:

A university decides that if a graduate student has achieved 120 credit points and has got a minimum of GPA as 2.0 then the student will get an unconditional offer to their masters by research course. If the student has achieved more than or equal to 150 credit points or has at least a GPA or 3.5 then a "conditional offer" will be given while in all other cases the student will be rejected. Write a python program that will accept the credit points and GPA from a user and will display accordingly the offer type.

Activity – 2:

A software company sells a package that retails for \$99. Quantity discounts are given according to the following table:

 Quantity Discount 10-19 10% 20-49 20% 50-99 30% 100 or more 40%

Write a program that asks the user to enter the number of packages purchased. The program should then display the amount of the discount (if any) and the total amount of the purchase after the discount.

Activity – 3:

Write a python program that will add the first 10 numbers (1 to 10). Change this program to get the total of only odd number between 1 and 10. Should this require a for loop or a while loop?

Activity - 4:

Write a python program that asks the user the sales amount and the commission rate and the program displays the commission accrued and the program keeps on asking the user this data as long they want to continue testing. Should this require a for loop or a while loop?

Activity - 5:

Write a python program that asks the user to enter a positive number and displays its digits in reverse order. E.g. if the user enters 35768, it must display 86753.

Should this require a for loop or a while loop?

Activity - 6:

Write a program to find the count of all odd number in the following list: [23, 9, 34, 2,78]. Should this require a for loop or a while loop?

Other interesting questions: https://www.w3resource.com/python-exercises/python- conditional-statements-and-loop-exercises.php