0% found this document useful (0 votes)
50 views

Mujtaba Baig Lab 6

This document discusses nested statements, break and continue statements in Python. It contains examples of code snippets with errors and asks the reader to identify and correct the errors. It also contains code snippets without errors and asks the reader to determine the output. Finally, it provides exercises to write Python programs using nested loops, break, continue and formatting strings to print multiplication tables and find prime numbers between two ranges.

Uploaded by

Mujtaba Baig
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

Mujtaba Baig Lab 6

This document discusses nested statements, break and continue statements in Python. It contains examples of code snippets with errors and asks the reader to identify and correct the errors. It also contains code snippets without errors and asks the reader to determine the output. Finally, it provides exercises to write Python programs using nested loops, break, continue and formatting strings to print multiplication tables and find prime numbers between two ranges.

Uploaded by

Mujtaba Baig
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

MUJTABA BAIGB FALL-2022-BSE-106

LAB # 06
NESTED STATEMENTS,
BREAK AND CONTINUE STATEMENTS

OBJECTIVE
Working on nested statements and control loop iteration using break and continue.

EXERCISE

A. Point out the errors, if any, and paste the output in the following Python
programs.

1. Code
prompt = "\nPlease enter the name of a city you have visited:"
prompt+="\n(Enter 'quit' when you are finished.)"
while True:
city = str(input(prompt))
if city == quit:
break;
else:
print("I'd love to go to " , city.title() , "!")

Error:

Output
Source Code:

Corrrection:
MUJTABA BAIGB FALL-2022-BSE-106

2. Code
if x>2:
if y>2:
z=x+y
print(“z is”, y)
else:
print(“x is”, x)

Output
Error:

Source code:

Correction:

3. Code
balance = int(input("enter yourbalance1:"))
while true:
if balance <=9000:
continue;
balance = balance+999.99
MUJTABA BAIGB FALL-2022-BSE-106

print("Balance is", balance)

Output
Error:

Source code:

Correction:

B. What will be the output of the following programs:

1. Code

Output

i = 10
if (i==10):
#First if statement
if (i < 15):
print ("i is smaller than 15")
# Nested - if statement
# Will only be executed if statement above
# it is true
if (i < 12):
print ("i is smaller than 12 too")
else:
print ("i is greater than 15")
MUJTABA BAIGB FALL-2022-BSE-106

2. Code
i = 1 i = 1
j = 2 j = 2
k = 3 k = 3
if i > j: if i > j:
if i > k: if i> k:
print('A') print('A')
else: else:
print('B') print('B')

Output

3. Code
# nested for loops
for i in range(0, 5):
for j in range(i):
print(i, end=' ')
print()

Output

C. Write Python programs for the following:

1. Write a program to add first seven terms twice of the following series:

Source code:
MUJTABA BAIGB FALL-2022-BSE-106

Output:

2. Write a program to print all prime numbers from 900 to 1000.


[Hint: Use nested loops, break and continue]
Source code:
output
MUJTABA BAIGB FALL-2022-BSE-106

3. Write a program to display multiplication table (1-5) using nested looping


Sampled output: [hint: '{ } ' .format(value)]
02 X 01 = 02
Source Code:

Output:

You might also like