Today, we are going to learn about loops. There are two kinds of the loop, first, where we know the number of times we have to perform the task and are known as a definite loop (e.g. for loop), secondly, the loops where we are unsure how many times we need to iterate the steps and are known as indefinite loop(e.g. while loop). We will see these two loops and their usage.
Let's start with for loop. Suppose we have a student, for whom we need to get the total marks obtained and calculates his percentage. This program will ask the student to enter marks for 5 subjects and assuming each subject was of maximum 100 marks.
total_marks = 0
for subject in range(5):
for subject in range(5):
marks = float(input("Enter the marks obtained in subject "
+ str(subject + 1) + ": "))
total_marks += marks
average = total_marks / 500
percentage = average * 100
print("Your total marks are: " + str(total_marks))
print("Your percentage is: " + str(percentage) + "%")
If we execute this, we will get a result something like below:
Enter the marks obtained in subject 1: 79
Enter the marks obtained in subject 2: 89
Enter the marks obtained in subject 3: 93
Enter the marks obtained in subject 4: 91
Enter the marks obtained in subject 5: 83
Your total marks are: 435.0
Your percentage is: 87.0%
Enter the marks obtained in subject 2: 89
Enter the marks obtained in subject 3: 93
Enter the marks obtained in subject 4: 91
Enter the marks obtained in subject 5: 83
Your total marks are: 435.0
Your percentage is: 87.0%
Let's now look at while loop. We use while loop to iterate the same steps, but we are not sure how many times we have to execute it, for example, we often see promotions like, offer valid till so and so date or until stock lasts. They are not sure how many people will avail the offer.
We will write a program to ask the user for a number and check if it is an even or odd number and then display it. It will prompt until the user quits.
Note: To check if a number is even or odd, we will have to divide it by 2 and check for its remainder. In order to do it, use the % operator.
num = input("Enter a number or q to quit: ")
while num != "q":
if int(num) % 2 == 0:
print(num + " is an even number")
else:
print(num + " is an odd number")
num = input("Enter a number or q to quit: ")
print("Thank you!")
while num != "q":
if int(num) % 2 == 0:
print(num + " is an even number")
else:
print(num + " is an odd number")
num = input("Enter a number or q to quit: ")
print("Thank you!")
Run this and keep trying with some numbers and check the result.
Enter a number or q to quit: 12
12 is an even number
Enter a number or q to quit: 15
15 is an odd number
Enter a number or q to quit: 44
44 is an even number
Enter a number or q to quit: 25
25 is an odd number
Enter a number or q to quit: q
Thank you!
12 is an even number
Enter a number or q to quit: 15
15 is an odd number
Enter a number or q to quit: 44
44 is an even number
Enter a number or q to quit: 25
25 is an odd number
Enter a number or q to quit: q
Thank you!
Hope you have enjoyed programming it. In our next post, we will play around with more controls. Stay tuned.
No comments:
Post a Comment