Write a Python program to perform the following tasks according to the user's choice using a menu.
a) Display the menu with four options: 1. Reverse a String, 2. Count Vowels, 3. Remove Duplicates, 4. Exit.
b) Ask the user to enter their choice and validate it. If the choice is invalid, display an error message and ask again.
c) If the choice is 1, ask the user to enter a string and then reverse and print it using slicing.
d) If the choice is 2, ask the user to enter a string and then count and print the number of vowels in it using a loop.
e) If the choice is 3, ask the user to enter a string and then remove and print the duplicate characters in it using a set.
f) If the choice is 4, exit the program with a goodbye message.
g) Use a loop to repeat the menu until the user chooses to exit.
Answer:
Answer by student
print(
"Menu"
)
print(
"1. Reverse a String"
)
print(
"2. Count Vowels"
)
print(
"3. Remove Duplicates"
)
print(
"4. Exit"
)
choice = int(input(
"Enter your choice: "
))
while
choice
not
in
[
1
,
2
,
3
,
4
]:
print(
"Invalid choice. Please try again."
)
choice = int(input(
"Enter your choice: "
))
while
choice !=
4
:
if
choice ==
1
:
s = input(
"Enter a string: "
)
reversed_s = s[::
-1
]
print(
"The reversed string is:"
, reversed_s)
elif
choice ==
2
:
s = input(
"Enter a string: "
)
vowels =
"aeiouAEIOU"
count =
0
for
c
in
s:
if
c
in
vowels:
count +=
1
print(
"The number of vowels is:"
, count)
elif
choice ==
3
:
s = input(
"Enter a string: "
)
no_duplicates =
""
.join(set(s))
print(
"The string without duplicates is:"
, no_duplicates)
print(
"\nMenu"
)
print(
"1. Reverse a String"
)
print(
"2. Count Vowels"
)
print(
"3. Remove Duplicates"
)
print(
"4. Exit"
)
choice = int(input(
"Enter your next choice: "
))
while
choice
not
in
[
1
,
2
,
3
,
4
]:
print(
"Invalid choice. Please try again."
)
choice = int(input(
"Enter your next choice: "
))
print(
"Thank you for using the program. Goodbye!"
)
Detailed answer by teachoo
-lock-
- The first part of the code is to display the menu with four options: 1. Reverse a String, 2. Count Vowels, 3. Remove Duplicates, 4. Exit.
- We use the print() function to print the strings inside the parentheses to the console. Each print() function prints a new line.
- The second part of the code is to ask the user to enter their choice and validate it.
- We use the input() function to take a string as an argument and return the user’s input as a string. For example, input("Enter your choice: ") will print "Enter your choice: " and wait for the user to type something and press enter.
- We use the int() function to convert the user’s input from a string to an integer. For example, int(“5”) will return 5 as an integer. This is necessary because we want to compare the user’s choice with numbers later.
- We use the while loop to repeat a block of code until a condition is false. For example, while choice not in [1, 2, 3, 4]: will repeat the code inside the loop as long as the choice is not one of the numbers in the list [1, 2, 3, 4].
- We use the not in operator to check if a value is not present in a sequence, such as a list or a string. For example, 5 not in [1, 2, 3, 4] will return True because 5 is not in the list.
- Inside the while loop, we print an error message using print() and ask the user to enter their choice again using input() and int().
- The third part of the code is to use another while loop to repeat the menu until the user chooses to exit.
- We use the while choice != 4: to repeat the code inside the loop as long as the choice is not equal to 4. We use the != operator to check if two values are not equal. For example, 5 != 4 will return True because 5 is not equal to 4.
- Inside this loop, we use an if-elif-else statement to perform different tasks based on the user’s choice.
- We use the if statement to check if a condition is true and execute a block of code if it is. For example, if choice == 1: will execute the code inside if choice is equal to 1. We use the == operator to check if two values are equal. For example, 5 == 5 will return True because 5 is equal to 5.
- We use the elif statement to check another condition after an if statement and execute a block of code if it is true. For example, elif choice == 2: will execute the code inside if choice is equal to 2 and only if the previous if statement was false.
- We use the else statement to execute a block of code if none of the previous conditions were true. For example, else: will execute the code inside if none of the previous if or elif statements were true.
So the final code is:
# Display the menu
print(
"Menu"
)
print(
"1. Reverse a String"
)
print(
"2. Count Vowels"
)
print(
"3. Remove Duplicates"
)
print(
"4. Exit"
)
# Ask the user to enter their choice and validate it
choice = int(input(
"Enter your choice: "
))
while
choice
not
in
[
1
,
2
,
3
,
4
]:
print(
"Invalid choice. Please try again."
)
choice = int(input(
"Enter your choice: "
))
# Use a loop to repeat the menu until the user chooses to exit
while
choice !=
4
:
# If the choice is 1, reverse and print the string using slicing
if
choice ==
1
:
s = input(
"Enter a string: "
)
reversed_s = s[::
-1
]
# Slicing is a way of accessing a part of a string using [start:stop:step] syntax.
# If start and stop are omitted, it means the whole string.
# If step is negative, it means going backwards from right to left.
# So s[::-1] means reversing the string by taking every character from right to left.
print(
"The reversed string is:"
, reversed_s)
# If the choice is 2, count and print the number of vowels using a loop
elif
choice ==
2
:
s = input(
"Enter a string: "
)
vowels =
"aeiouAEIOU"
count =
0
for
c
in
s:
if
c
in
vowels:
count +=
1
# A loop is a way of repeating a block of code for a certain number of times or until a condition is met.
# For loops are used to iterate over a sequence, such as a string or a list.
# In this case, we use a for loop to go through each character in the string s.
# We also use an if statement to check if the character is in the string vowels, which contains all the vowels in both cases.
# If it is, we increment the count variable by one using count += 1, which is equivalent to count = count + 1.
# After the loop ends, we print the value of count, which is the number of vowels in s.
print(
"The number of vowels is:"
, count)
# If the choice is 3, remove and print the duplicate characters using a set
elif
choice ==
3
:
s = input(
"Enter a string: "
)
no_duplicates =
""
.join(set(s))
# A set is a data structure that stores unique and unordered elements.
# We can convert a string to a set using the set() function, which will remove any duplicate characters.
# However, a set is not a string, so we need to convert it back to a string using the join() method.
# The join() method takes a sequence of strings and joins them together using a separator, which can be an empty string "".
# So "".join(set(s)) means joining the elements of the set s with no separator, which gives us a string without duplicates.
print(
"The string without duplicates is:"
, no_duplicates)
# Display the menu again and ask for the next choice
print(
"\nMenu"
)
print(
"1. Reverse a String"
)
print(
"2. Count Vowels"
)
print(
"3. Remove Duplicates"
)
print(
"4. Exit"
)
choice = int(input(
"Enter your next choice: "
))
while
choice
not
in
[
1
,
2
,
3
,
4
]:
print(
"Invalid choice. Please try again."
)
choice = int(input(
"Enter your next choice: "
))
# If the choice is 4, exit the program with a goodbye message
print("Thank you for using the program. Goodbye!")
-endlock-