Hello! I have a program that creates a list in which each element is a string retrieved via user input. The user continues entering input until ‘q’ is entered to break the while loop and return the list to main(). No issues there.
That list is then passed to the next function as a parameter, where it is iterated to calculate the number of uppers in each string element. I was able to return the correct result, technically, using a for loop and isupper(). But it’s confusing and needs to be fixed.
Code:
def get_strings():
''' take input until input = q beaks loop, store inputs in list,
return list to main() '''
l_phrases= []
ask_again = 1
while ask_again == 1:
phrases = input("Enter your words/phrases, or q to quit, here: ")
l_phrases.append(phrases)
if phrases == "q":
ask_again = 0
break
return l_phrases
def count_caps(l_phrases):
''' take input list as parameter, iterate list to tally number of uppers,
store number in new list, display new list '''
num_caps = []
for words in l_phrases:
caps = sum(1 for letters in words if letters.isupper())
num_caps.append(caps)
print(num_caps)
def main():
''' print elements from list of inputs on separate lines,
call functions '''
l_phrases= get_strings()
for p in l_phrases:
print(p)
count_caps(l_phrases)
main()
For input:
Enter your words/phrases, or q to quit, here: How
Enter your words/phrases, or q to quit, here: You
Enter your words/phrases, or q to quit, here: Doin
Enter your words/phrases, or q to quit, here: q
Output:
How
You
Doin
q
[1]
[1, 1]
[1, 1, 1]
[1, 1, 1, 0]
As I said, technically correct. I would like to understand how I can achieve just displaying the string elements and the final list of uppercase counts in each of those elements.
My desired output:
How
You
Doin
q
[1, 1, 1, 0]