[go: up one dir, main page]

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

Week 5

The document outlines a Python assignment that includes functions to display a specified number of characters from a name, count the vowels in the name, and reverse the name. It describes the main function that initializes the name, prompts user input, and calls the defined functions to perform the operations. Additionally, it explains the execution control to ensure the main logic runs when the script is executed directly.

Uploaded by

falakfraidoon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views3 pages

Week 5

The document outlines a Python assignment that includes functions to display a specified number of characters from a name, count the vowels in the name, and reverse the name. It describes the main function that initializes the name, prompts user input, and calls the defined functions to perform the operations. Additionally, it explains the execution control to ensure the main logic runs when the script is executed directly.

Uploaded by

falakfraidoon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Python assignment week 5:

# Function to display n characters from the left


def display_n_characters(name, n):
print("Displaying", n, "characters from the left:", name[:n])

# Function to count the number of vowels


def count_vowels(name):
vowels = 'aeiouAEIOU'
vowel_count = 0
for char in name:
if char in vowels:
vowel_count += 1
print("Number of vowels in the name:", vowel_count)

# Function to reverse the name


def reverse_name(name):
reversed_name = name[::-1]
print("Reversed name:", reversed_name)

# Main function
def main():
# Your name
name = "Fraidoon Falak"

# Displaying name
print("my name:", name)

# Input for the number of characters to display from left


n = int(input("Enter the number of characters to display from left: "))

# Displaying n characters from left


display_n_characters(name, n)

# Counting vowels
count_vowels(name)

# Reversing the name


reverse_name(name)

# Calling the main function


if __name__ == "__main__":
main()
Output of the code:

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

You might also like