Nested Loops (Autosaved)

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 10

Nested Loops

• A nested loop is any type of loop inside an


already existing loop. They can involve
any type of loop. For this, we will use For
loops. It is important to remember that the
inner loop will execute its normal amount
multiplied by how many times the outer
loop runs. For example:
Python code
for i in range(0,10):
for j in range(0,2):
print ("hi”)
• Question
1. Create a program which will print a triangle of stars,
given the size by the user(edit for this). For Example: if
the size was 3 it would  print:

*
**
***
for x in range(0,10):

for y in range(0,x):

print("*",end=" ")
print()
• Write program to print following pattern:

**********
**********
**********
**********
for x in range(0,10):

for y in range(0,10):

print("*",end=" ")
print()
Q1
Some students have been carrying out 2
experiments; 5 temperature readings were
taken in each experiment
Write a program code, which inputs all the
temperature readings, and outputs the
average temperature for each experiment, the
average temperature for all 2 experiments and
the highest temperature recorded from all the
readings taken.
• max=0
• tt=0
• for i in range(0,2):
• total=0
• for j in range(0,5):
• a=int(input("enter temperature"))
• total=total+a
• tt=tt+a
• if a > max:
• max=a
• print("aVERAGE IS",total/5)
• print("MAXIMUN IS ",max)
• print("total average is ",tt/10)
highest=0
totalAll=0
for expt in range(0,2):
totalExp=0
for reading in range(0,5):
temp=int(input("Temperatue "))
totalExp = totalExp +temp
totalAll = totalAll + temp
if temp > highest:
highest = temp
averageExp = totalExp / 5
print averageExp

averageAll = totalAll / 10
print averageAll

You might also like