Home > other >  I am trying to run this code that asks user to enter a sentence, the display the number of vowels an
I am trying to run this code that asks user to enter a sentence, the display the number of vowels an

Time:11-18

I am getting syntax errors when trying to run or sometimes it runs but does not execute the way I am intending it to.

I have been playing around with the formatting but still no solution.

def checkVowelsConsonants(s):
    vowels=0
    consonants=0
    for ch in s:
#convert character into its ASCII equivalent
        ascii_value=ord(ch)
#if ASCII is between 65 to 90 to 97 to 122 then it's a character
#otherwise a special character
    if((ascii_value>=65 and ascii_value<=90)or(ascii_value>=97 and ascii_value<=122)):
#check for lower case
        if ch=='a' or ch=='e' or ch=='i' or ch=='o' or ch=='u':
            vowels=vowels 1
#check for upper case
    elif ch=='A' or ch=='E' or ch=='I' or ch=='O' or ch=='U':
            vowels=vowels 1
    else:
        consonants=consonants 1
#print the result
        print("The number of vowels is " str(vowels) " and consonants is " str(consonants))

while True:
#print the menu
        print("1. Print the number of vowels and consonats")
        print("2. Exit the program")
#take choioce as input from user
        choice=int(input("Enter choice: "))
#take sentence input from user
        if choice==1:
            sentence=input("Enter a sentence: ")
sentence_list=[]
for ch in sentence:
    sentence_list.append(ch)
    checkVowelsConsonants(sentence_list)
#exit the program
if choice==2:
    break
#choice other that 1 and 2
else:
    print("Invalid choice!")

CodePudding user response:

I've cleaned up your code, checking ascii values with 65 <= ascii_value <= 90. As you want to check lowercase and upper case vowels I made this one if condition by checking if ch in "aeiouAEIOU" making all other valid characters lowercase or uppercase consonants.

It is also not necessary to convert your user input sentence to a list prior to your checkVowelsConsonants call, as a string is also iterable. That is why I removed that for loop appending each individual character. But if that was intended by you, just leave as is.

By following your code I indented it so that your while loop only terminates when choice is 2.

def checkVowelsConsonants(s):
    vowels = 0
    consonants = 0
    for ch in s:
        # Convert character into its ASCII equivalent
        ascii_value = ord(ch)
        # If ASCII is between 65 to 90 or 97 to 122 then it's a character
        # otherwise a special character
        if (65 <= ascii_value <=90) or(97 <= ascii_value <= 122):
            # Check for lower case and upper case vowels
            if ch in "aeiouAEIOU":
                vowels = vowels   1
            else:
                consonants = consonants   1
    # Print the result
    print("The number of vowels is "   str(vowels)   " and consonants is "   str(consonants))

while True:
    # Print the menu
    print("1. Print the number of vowels and consonats")
    print("2. Exit the program")
    # Take choice as input from user
    choice = int(input("Enter choice: "))
    # Take sentence input from user
    if choice == 1:
        sentence = input("Enter a sentence: ")
        checkVowelsConsonants(sentence)
    # Exit the program
    elif choice == 2:
        break
    # choice other than 1 or 2
    else:
        print("Invalid choice!")

CodePudding user response:

To add:

The program doesn't end even with selection of choice 1. So, here I have rewritten the code and added a break after the choice input. I have also used string formatted text to keep things a bit cleaner. Here below:

def checkVowelsConsonants(s):
    vowels = 0
    consonants = 0
    for ch in s:
        ascii_value = ord(ch)
        if ((ascii_value >= 65 & ascii_value <= 90) | (ascii_value >= 97 & ascii_value <= 122)):
#check for lower case
            if ch == 'a' or ch == 'e' or ch == 'i' or ch == 'o' or ch == 'u':
                vowels = vowels 1
#check for upper case
            elif ch == 'A' or ch == 'E' or ch == 'I' or ch == 'O' or ch == 'U':
                vowels  =1
            else:
                consonants  =1
#print the result
    print("The number of vowels is {} and consonants is {}".format(vowels,consonants))

    
while True:
#print the menu
    print("Choices:")    
    print("1: Print the number of vowels and consonants")
    print("2: Exit the program\n")
#take choioce as input from user
    choice = int(input("Enter choice: \n"))
#take sentence input from user
    if choice == 1:
        sentence = input("Enter a sentence: \n")
        checkVowelsConsonants(sentence)
        break
#exit the program
    elif choice == 2:
        print('Program exited.')
        break
#choice other that 1 and 2
    else:
        print("Invalid choice!")
  • Related