Lab 04
Lab 04
Lab #4
Dr Asad Mansoor Khan
LE Kashaf Raheem
Algorithms and
Repetition
Aim
Exploring python syntax and problem-solving w.r.t repetition
structures
2
Example: Change example
Question Solution
• Write a program that reads 10 count = 0
positive numbers from the keyboard
and determines and displays the sum sum = 0
and average of the numbers while (count < 10):
number = input("Input a new
number: ");
sum = sum + int(number)
count = count + 1
4
Looping “Forever”
Problem Solution
• Use a value that will always be true while True:
to loop until a “break”.
string = input()
if string==“exit”:
break
print (string)
print ("Done”)
5
Tasks
Make sure to comment your code.
Use appropriate variable name
Know the location where file is being saved
Create different files for different tasks
6
Task 1:
• Write
a program that keeps getting
and adding the number (unless it is
multiple of 5) until negative
number is entered. Display the final
sum as output.
8
String and loops
9
Task 3:
Write a Python program that takes two
strings as input and prints the number of
instances of the second string in the first
string.
Example:
first_string = “Today is second of the month”
second_string = “o”
Output = 4
Next week: Python
Syntax and Problem
Solving!
Advice:
Explore Leetcode for problem solving!
16