[go: up one dir, main page]

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

Lab 4 Manual - Loops

Uploaded by

yashfa ali
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)
57 views3 pages

Lab 4 Manual - Loops

Uploaded by

yashfa ali
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/ 3

What We Have Learned So Far

● Data Types and Variables: How to store and manipulate data using different types (e.g.,
integers, strings, etc.).
● Taking Input and Displaying Output: Using input() to get user input and print()
to display information.
● Performing Arithmetic Operations: Using operators like +, -, *, / to perform
calculations.
● Decision Making with Conditions: Using if, if-else, and if-elif-else to control
the flow of the program based on conditions.
● Writing Conditions: Using ==, !=, <, > and boolean expressions (AND, OR and NOT)
to write conditions for decision-making.

Sample Code
# Sample to review what we've learned so far
age = int(input("Enter your age: "))
if age >= 18:
print("You are eligible to vote.")
else:
print("You are not eligible to vote.")

Understanding the Repetition Use Case


Imagine you need to solve a problem where a task must be repeated multiple times. For
example:

● Asking for user input until they provide valid data.


● Iterating over a list of items.
● Counting from 1 to n (where n is provided by the user).

With the tools you've learned so far, you can't easily handle such problems. That's where loops
come in. Loops allow you to repeat a block of code several times, making it possible to solve
these kinds of problems.

While Loop Syntax

The while loop repeats as long as a condition is True.


while condition:
# code to execute

For Loop Syntax

The for loop iterates over a sequence (like a list, string, or range).

for variable in sequence:


# code to execute

Example 1: Keep Asking for Input Until a Negative Number is Entered

num = int(input("Enter a number: "))


while num >= 0:
print("You entered:", num)
num = int(input("Enter another number: "))
print("You entered a negative number, exiting.")

Example 2: Printing Counting from 1 to n (n is User Input)

n = int(input("Enter a number: "))


for i in range(1, n+1):
print(i)

Example 3: Print All Characters of a String

name = input("Enter a string: ")


for char in name:
print(char)

Example 4: Calculating the Sum of n Numbers

n = int(input("Enter the number of items: "))


total = 0
for i in range(n):
number = int(input("Enter a number: "))
total += number
print("Total sum is:", total)

Lab Questions
1. Write a program that asks the user to enter a password repeatedly until they enter the
correct one. You can define your correct password in code as a hardcoded value.
2. Create a program that prints the first n even numbers, where n is input by the user. For
example, if a user enters 5, the program will print 2 and 4.
3. Write a loop that counts backward from a user-provided number down to zero and
displays it. For example if user enters 5, the output would be
5
4
3
2
1
4. Create a program that asks for five test scores and then calculates and prints the
average. Valid test score is between 0 and 100. Your program should keep asking for
test scores till the user enters a valid score.
5. Write a program to print all the vowels in a given string.
6. Write a program that takes n numbers as input where n is also a user input and it
displays only even numbers.
7. Write a program that prints a multiplication table for numbers from 1 to 10.
8. Write a program that asks the user for a list of names and prints each name in
uppercase. You can use the .upper() function in python to make the names uppercase.
9. Write a program that simulates a guessing game where the user has to guess a number
between 1 and 10, and the game keeps running until the correct number is guessed. You
can hardcode the correct number.

You might also like