Lab 4 Manual - Loops
Lab 4 Manual - Loops
● 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.")
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.
The for loop iterates over a sequence (like a list, string, or range).
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.