Changing output to only one line

Hello, I’m still a beginner to Python coding.
Currently, I’m stuck at this part where I’m trying to generate ONLY one line of output that contains the answers.
image

Instead of repeating the ‘Vowels are:’ output, I want my output to just be:
Vowels are: [ans1] [ans2] …

Also, my side question is how can i opt out the same answer, like in this case with 4 u’s?

Here’s my code:

yourSentence = input(“Enter your sentence here:”).lower()
numberofVowels = set(‘aeiou’)
number=set(‘0123456789’)
count = 0
counter = 0
counters = 0
counterss = 0
for x in yourSentence:
if x in numberofVowels:
count += 1
elif x in number:
counterss += 1
elif x == ’ ‘:
continue
elif not x.isalpha():
counters += 1
else:
counter += 1
print(“No. of Numbers:”, counterss)
print(“No. of vowels:”, count)
print(“No. of special:”, counters)
print(“No. of consonants:”, counter)
#stuck-part
for y in yourSentence:
if y in numberofVowels:
print(“Vowels are:”, y, end=’ ')
Thank u.

Hello, @pmuny. You can just add vowels to a list and then print it to achieve that. You can see the code below:

#stuck-part
#ADDING VOWEL LETTERS TO A LIST:
#option1:
vowelsFound = []
for y in yourSentence:
	if y in numberofVowels:
		vowelsFound.append(y)
#option 2:
#vowelsFound = [i for i in yourSentence if i in numberofVowels]

#PRINTING YOUR LIST:
#option 1:
print( "Vowels are: "+ ",".join(vowelsFound) )
#option 2:
#print("Vowels are:",end=" ")
#index = 0
#for i in vowelsFound:
#	index += 1
#	if index != (len(vowelsFound)-1):
#		print(i,end=",")
#	else:
#		print(i)

Note: You can also think about your variable names. They are so confusing and it may be hard to understand what they are doing. You can re-name them like specialCharNo, numberNo etc.

Note: You can make your code highlighted and indent-reserved by creating the formation below:

```
Your Code Here!
```

1 Like

Your code initially confused me quite a bit, as besides not being in a code block (and thus not only hard to follow, but impossible to parse without guessing where the syntactically critical indentation was supposed to go), the names were not only not very descriptive (count, counter, counters and counterss all meaning different things), but also in some cases actively confusing, e.g. numberOfVouls is not, as its name would imply, a number at all, but rather a list of all the vowels, which was only obvious after carefully picking through your code. You might want to rename it something like all_vowels instead.

In any case, there is one potential issue with @sandraC 's solutions—while not explicitly specified in your question (in the future, remember to describe what exactly your desired output is, with examples), I would presume you only want to print the unique values used, not every vowel (i.e. for the sentance
“Spam spam eggs and spam” you’d want to print Vowels are: a, e not Vowels are: a, a, e, a, a. If that’s the case, you can just call set() on your vowelsFound list prior to printing it to make it a set, which will only retain the unique vowels, or just make it a set to begin with).

However, you can make your code much shorter, simpler, more readable and more efficient by redesigning it (or “refactoring”, as we call it) a bit. Instead of iterating through your sentence to count the number of each character, keeping track of a bunch of counters, and then iterating through again to get the unique vowels, I suggest just iterating through it once and adding each character found its respective list based on its type. Then, you can get the count when you need it by calling len() on the list, all the vowels (or consonents, etc) by printing the list, and the unquie vowels by casting the list to a set and printing that.

Also, you aptly used str.isalpha() to check for non-vowel letters, but you can equally well use str.isdecimal() to check for decimal numbers characters rather than hardcoding them (or using string.digits, which is a build-in iterable string of the ASCII digits). And instead of a hardcoded check for only space (" "), use str.iswhitespace().

Assuming you have

ALL_VOWELS = set("aeiou")
your_sentence = input("Enter your sentence here: ").lower()

and create your lists like so,

digits_used = []
vowels_used = []
consonants_used = []
specials_used = []

you could then loop through your sentence like this:

for char in your_sentence:
    if char.isspace():
        continue
    elif char.isdecimal():
        digits_used.append(char)
    elif char in ALL_VOWELS:
        vowels_used.append(char)
    elif char.isalpha():
        consonants_used.append(char)
    else:
        specials_used.append(char)

Print the counts like this:

print("Number of vowels:", len(vowels_used))
# Etc for the rest

and then, to print the specific, unique vowels used, just do this:

print("Vowels are: ", ", ".join(set(vowels_used)))

That’s shorter, simpler and much easier to keep track of than two loops and lots of counters, and also lets you easily modify your code to print the other character types as well, get counts of each specific vowel, etc.

To note, aside from @sandraC 's excellent advice to use more descriptive names, which is indeed very important (and more so than the style you use), per the PEP 8 standard and widely used Python convention, sentence_case is used for variable names (and function/method names), while (upper) CamelCase is used for class names (i.e. number_of_vowels, vowels_found).

2 Likes

Thank you, Sandra and Gerlach!