11.5k views
4 votes
Write a Python function LetterGame() that repeatedly asks the user to input a letter. The function is to count the number of vowels the user entered. The function should stop if the user enters a digit (0-9). a) Use a while-loop and in the while loop ask the user to input a letter or to input a digit to stop. b) Check if the user entered a vowel (if command is your friend) c) If the user entered a vowel increase the counter by one d) If the user entered a digit, output the number of letters the user entered and the number and percentage of vowels among them. e) Call the function. Expected output: You entered 10 letters, 2 of which were vowels. The percentage of vowels was 20%.

1 Answer

5 votes

Answer:

def LetterGame():

vowels = ["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"]

count1 = count2 = 0

while True:

choice = input("Enter a letter / digit to stop: ")

if choice.isdigit():

break

elif choice.isalpha():

count1 += 1

if choice in vowels:

count2 += 1

print("You entered " + str(count1) + " letters, " + str(count2) + " of which weere vowels.")

print("The percentage of vowels was " + str(count2 / count1 * 100) + "%")

LetterGame()

Step-by-step explanation:

Create a function called LetterGame

Inside the function:

Create a list of vowels

Initialize count1, counts the total letters, and count2, counts the vowels

Initialize a while loop that iterates until the specified condition is met in the loop

Get the input from the user. If it is a digit, stop the loop. If it is an alphabet, increase the count1 by 1, and also check if it is a vowel. If it is a vowel, increment the count2 by 1

When the loop is done, print the required information

Finally, call the function

User Raees Iqbal
by
5.2k points