Explanation
To complete the first part of the question, I assigned my name to a local variable and then output it using the print function in
a functional string.
For the second part, I used a try-except block to get the user’s input and assign it to a variable n which I initially declared
with a zero. The try-except block prevents the program from stopping in the case of a value error. In the body of the except
section I print out the problem for the user to tell what went wrong then I restart the function. I also added an if block to
check if the value is greater than the length of my name and restart the function if so with an error message indicating where
the problem is.
For the third part, I used 0 and n as the range of characters to obtain from my name. However, instead of using the spaced
name variable, I created a name without spaces variable to apply the range to. I then assigned this to the first characters
variable which I output using the print function.
For the last part, I created the highest index variable and assigned to it the value of the name minus one since strings use a
zero indexing system in Python. I created a reversed name variable and then created a for loop that would iterate the length of
the string. To make the string reversed I added the character at the index equal to the highest index variable to the reversed
name variable then subtracted one from the highest index variable. Finally, I used a print function to output the reversed
name.
Code
import re
def name_modifier():
#Declaring my name and it's length as local variables
name = "Ovuvuevuevue Enyetuenwuevue Ugbemugbem Osas"
name_len = len(name)
#Displaying my name and the number of characters it has
print(f"My name is {name}.\n"
"I want us to do some amazing things with my name.\n"
"First you will see the first letters of my name. (You get to decide how many)\n"
f"Note: My name has only {name_len} characters so please don't input a greater number")
#Getting users input for the value of n
#Creating a try and catch block to prevent a value error incase the user inputs a string with letters
n=0
try:
n = int(input("Please input the number(in digits) of letters you would like to see: "))
except ValueError:
print("You did not follow the instructions. Let us start again to see if you can get it right this time")
name_modifier()
#Restarting the function incase the user give a greater value than the length of the name
if n > name_len:
print("You did not follow the instructions. Let us start again to see if you can get it right this time")
name_modifier()
#Getting the characters available within the range 0 - n to display
name_without_spaces = "OvuvuevuevueEnyetuenwuevueUgbemugbemOsas"
first_characters = name_without_spaces[0:n]
#Outputing the first n characters of my name
print(f"First {n} letter(s) of my name: {first_characters.lower()}\n")
#Creating a regex to match the vowels
vowel_regex = [Link]('[aeiou]+')
#Testing for the presence of vowels in my name
vowels = vowel_regex.findall([Link]())
#Outputing the vowels present
print("FUN FACTS")
print("The vowels in my name are:")
#Creating a list for the vowels
count = 1
for vowel in vowels:
if len(vowel)> 1:
# Separating consecutive vowels into individual list items
for l in vowel:
print(f"{count}. {l}")
count += 1
else:
print(f"{count}. {vowel}")
count += 1
#Reversing my name
highest_index = name_len - 1
reversed_name = ""
for i in range(name_len):
reversed_name += name[highest_index]
highest_index -= 1
#Output reversed name
print("My name in reverse is: " + reversed_name.lower())
name_modifier()
Output
References
[Link] (2001) Regular Expression HOWTO. Retrieved from
[Link]
howto