0% found this document useful (0 votes)
5 views10 pages

Lab 04

Uploaded by

hsiddiqui011
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views10 pages

Lab 04

Uploaded by

hsiddiqui011
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Applications of ICT

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

print ("Sum of 10 numbers is: ", sum)

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.

Hint: Make use of nested if/else within


while
7
Task 2:
•Write a Python program
that accepts an integer (n)
and computes the value of
𝒏 + (𝒏 + 𝒏) + (𝒏 + 𝒏 + 𝒏) +
… (𝒏 + 𝒏 + 𝒏 … + 𝒏)

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

You might also like