ISY104 Repetition Control Structure: Activity Sheet Week 4 Loops Assessment Answer

pages Pages: 4word Words: 890

Question :

ISY104 – Foundations of Programming Week 4 – Loops


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 SegmentsAnswerReasoning
1while True: print(“hello”)hello
on infinite number of lines. The program does
not terminate
Does not terminate because the while loop condition never becomes False
2x =3 while True:
x= x-1
if x == 0: break
The loop terminates when x is 0 even though it has a while True because of the break statement within the loop
3x = 3 while x>0:
print(x) x = x -1
The while loop terminates when is x is 0.
4#assume user enters -3 first time and 5 the second time during an execution
while True:
x = int(input("Enter a positive number\n"))
if x >=0:

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 executes
secretNos=[2, 4, 7]#01
while True:#02
guess = int(input("Guess a secret number between 1 and 7\n")) #03
if guess in secretNos:#04 print("Correct guess")#05
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 if
value in guess exists in the list. It breaks the loop if it exists.
6for aVal in range(3): print(aVal)0
The range() function in Python creates a sequence of numbers from

7for aVal in range(3, 0, -1): print(aVal)3
The range() function in Python creates a sequence of numbers from
3 to 1. The for loop is used to iterate through each value in the sequence.
8for aVal in range(3, 1, -1): print(aVal)3
Similar reasoning
9for aVal in range(5, 7, 1): print(aVal)5
Similar reasoning
for i in range(1,4,1): f= f*i

The factorial of 3 is got.
for i in range(1,5,1):
s= s+i print(s)

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:

100 or more40%

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: conditional-statements-and-loop-exercises.php 

Show More

Answer :

For solution, connect with our online professionals.