0% found this document useful (0 votes)
8 views3 pages

Lab - Week 4 (2021)

İTÜ BIL100E notları

Uploaded by

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

Lab - Week 4 (2021)

İTÜ BIL100E notları

Uploaded by

erdemsimsek234
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 3
Repetition Structures Int lab session, you are going to learn; Repetition structures, including: © Condition-controlled loops © Count-controlled loops © Nested loops * Infinite loops and how they can be avoided range function as used in for loops. © Calculating a running total and augmented assignment operators 1) Consider following while statements and see the results. Infinite loop, exit with Ctr1+c while True: print(“Hello Python") Hanile Loop count = @ while (count < 9): print(‘The count is:', count) count = count +1 print ("6ood bye!") #unile loop with else mycount. while mycount < 5: print(mycount, "is less than 5") mycount = mycount + 1 else: print(mycount, " is not less than S") 2) This problem averages test scores. It asks the user for the number of students and the number of test scores per student. # Get the number of students. num_students = int(input('How many students do you have? *)) # Get the number of test scores per student. nun_test_scores = int(input(‘How many test scores per student? ')) # Determine each student's average test score. for student in range(num_students): # Initialize an accumulator for test scores. total = 0.0 # Get a student's test scores. print('Student number’, student + 1) print(* ) for test_num in range(nun_test_scores): print (‘Test nunber', test_num + 1, end= float(input(": *)) score = # Add the score to the accumulator. total += score # Calculate the average test score for this student. average = total / num_test_scores # Display the average print('The average for student number’, student + 1, 'is:', average) print() 3) Consider following for statement and see the results. For loop for letter in ‘Python’: print(‘Current Letter :', letter) fruits = [‘banana’, ‘apple’, ‘mango ] for fruit in fruits print(‘Current fruit :', fruit) print("éood bye!") 4) Consider following examples about nested loops and see the results. Nested for loop, the multiplication table for x in range(1, 11): for y in range(1, 11. print('d * Xd = xd" % (x, y, xty)) ‘Nested while loop x= -20 y= 20 while x <= yz print ("X is now: 2x) x=x+1 while x < @: print ("X is negative") xexed

You might also like