Programming Assignment Unit 5
Full name: Hassan Agha Hussainy
1. Display n Characters from the Left
Explanation
1. Accepting User's Name:
- The input() function is used to get the user's name. This function returns a string.
- The input is stored in the variable name.
2. Loop for Valid Input:
- A while True loop is used to continuously prompt the user for input until a valid number
is entered.
- Inside the loop:
o A try block attempts to convert the user's input into an integer using int(input(...)).
o If successful, the integer is stored in n.
o If not successful (e.g., if the user enters a string), a ValueError exception is raised,
and the code in the except block is executed.
3. Checking Input Validity:
o After converting the input to an integer, the code checks if n is less than or equal
to the length of the name (len(name)).
o If n is valid, the loop breaks, and the program continues.
o If n is greater than the length of the name, the user is prompted to enter a smaller
number.
4. Printing the First n Characters:
- Once a valid n is entered, the program prints the first n characters of the name. However,
there was a mistake in the code: it prints name[:n]. So, I changed to prints name[:n+1]
because when I enter 4, it displayed 3 letters.
Counting Vowel in the Name
Explanation
1. Accepting User's Name:
o The input() function is used to get the user's name. This function returns a string.
o The input is stored in the variable name.
2. Defining Vowels:
o A string vowel is defined containing all lowercase and uppercase vowels.
3. Counting Vowels:
o The code uses a generator expression within the sum() function to count the
number of vowels in the name.
Here's how it works:
- for char in name: This part iterates over each character in the name.
- if char in vowel: This checks if the current character is in the vowel string.
- sum(1 for ...): If the character is a vowel, it counts as 1; otherwise, it counts as 0. The
sum() function adds up all these counts to give the total number of vowels.
Printing the Count:
Finally, the code prints the total count of vowels found in the name.
Reversing the name:
Detailed Explanation
1. Accepting User's Name:
o The input() function is used to get the user's name. This function returns a string.
o The input is stored in the variable name.
2. Reversing the Name:
o The code uses Python's slicing feature to reverse the string.
o The syntax name[::-1] means:
name[start:stop:step]: This is the general form of slicing in Python.
start: Omitted, which means it starts from the beginning.
stop: Omitted, which means it goes to the end.
step: -1, which means it moves backwards through the string.
So, name[::-1] effectively reverses the string.
3. Printing the Reversed Name:
o Finally, the code prints the original name and its reversed form.
Example Output
If you enter "Mohammad Ali", the output should be:
Enter your name: Mohammad Ali
Reversed form of your name Mohammad Ali is: ilA dammahoM
Explanation of the Output
In the name "Mohammad Ali", when reversed, it becomes "ilA dammahoM". This is because the
characters are rearranged in reverse order: