Week 5
Week 5
# Main function
def main():
# Your name
name = "Fraidoon Falak"
# Displaying name
print("my name:", name)
# Counting vowels
count_vowels(name)
Code explanation:
1. Function Definitions:
display_n_characters: This function takes two arguments, name and n (the
number of characters to display from the left). It prints the specified number of
characters from the left side of the name.
count_vowels: This function takes name as input and counts the number of
vowels in the name. It iterates through each character of the name and increments
a counter if the character is a vowel.
reverse_name: This function takes name as input and reverses it using slicing
([::-1]). It then prints the reversed name.
2. Main Function (main):
The main function initializes the name variable with the value "Fraidoon
Falak", which is my name.
It prints the original name.
It prompts the user to input the number of characters to display from the left side
of the name (n).
It calls the display_n_characters function to display the specified number of
characters from the left.
It calls the count_vowels function to count the number of vowels in the name.
It calls the reverse_name function to reverse the name and print the result.
3. User Input and Output:
The user is prompted to enter the number of characters to display from the left.
The program then displays the original name, the specified number of characters
from the left, the count of vowels in the name, and finally, the reversed name.
4. Execution Control:
The final part of the code checks if the script is being run as the main module (if
__name__ == "__main__":). If it is, it calls the main function, ensuring that the
program's main logic is executed when the script is run directly.
Overall, this code takes your name, performs several operations on it , and provides user
interaction for inputting the number of characters to display.
References:
Downey, A. (2015). Think Python: How to think like a computer scientist. Green Tree Press.
Python Software Foundation. (2021). Python Language Reference, version 3.9. Available at
Python.org.
https://www.python.org