A | Assignment |
Loops in Python
Please save your solutions for Exercises 1 to 4 in a single Python script named unit06__ex(1-4)code.py
.
For Bonus Exercise, use a separate script named unit06__ex5code.py
.
Save both scripts in the same unit06
folder, compress the folder into a .zip
file, and upload it to ILIAS.
For more information, please visit the following link:
https://geomoer.github.io/moer-base-python/unit00/unit00-04_submission_guidelines.html
Make sure your code is clearly structured and includes comments where helpful.
Introduction
This set of tasks is designed so that each step builds on the previous one. Work through them in order to practice your understanding of loops and list manipulation in Python.
Task 1: Create a List of Numbers
Instructions:
- Create a list called
numbers
that contains every third number from 3 to 30 using afor
loop andrange()
. - Print the list.
Task 2: Filter the List
Instructions:
- Use the
numbers
list created in Task 1. - Create a new list called
filtered_numbers
containing only the numbers greater than 15. - Print the new list.
Task 3: Convert to Strings with Tags
Instructions:
- Take the
filtered_numbers
list from Task 2. - Convert each number into a string and add the prefix
"Value: "
to each element. - Store these values in a list called
tagged_numbers
and print it.
Task 4: Enter Numbers, Find Maximum and Minimum, and Sort the List
Description:
Write a program that asks the user to enter 5 numbers, stores them in a list, finds the minimum and maximum numbers, and outputs the sorted list.
Explanation:
- Initialize an empty list:
number_list
is initialized as an empty list to store the entered numbers. - Loop: The
while
loop runs as long as theattempt
is less than or equal to 5. - Enter numbers: Inside the loop, the user is asked to input a number, which is then added to the
number_list
usingappend()
. - Find minimum and maximum: After all numbers are entered, the minimum is found using
min(number_list)
and the maximum usingmax(number_list)
. - Sort the list: The list
number_list
is sorted using thesort()
method. - Output: The stored numbers, the minimum, and the maximum are displayed.
Task 5: Bonus - Password Input with Limited Attempts
Description:
Write a program that implements a password prompt. The program asks the user for a password and allows them to guess up to five times. If the correct password is entered, the program displays a success message. After five incorrect attempts, the program displays an error message.
Explanation:
- Correct password: The correct password is defined.
- Initialize variables: The number of attempts is set to 0, and the maximum number of attempts is set to 5.
- Loop: The
while
loop runs as long as the number of attempts is less than the maximum number of attempts. - Password input: Inside the
while
loop, the user is asked to input the password. - Password verification: The entered password is checked.
- If the password is correct, a success message is displayed and the loop is terminated using
break
. - If the password is incorrect and the number of attempts has not been exhausted, an error message is displayed.
- If the number of attempts is exhausted, an error message is displayed stating that too many incorrect attempts have been made.
- If the password is correct, a success message is displayed and the loop is terminated using