134k views
5 votes
Write an application that accepts up to 20 Strings, or fewer if the user enters the terminating value ZZZ. Store each String in one of two lists—one list for short Strings that are 10 characters or fewer and another list for long Strings that are 11 characters or more. After data entry is complete, prompt the user to enter which type of String to display, and then output the correct list. For this exercise, you can assume that if the user does not request the list of short strings, the user wants the list of long strings. If a requested list has no Strings, output The list is empty. Prompt the user continuously until a sentinel value, ZZZ, is entered.

1 Answer

5 votes

Answer:

count = 20

i = 0

short_strings = []

long_strings = []

while(i<count):

s = input("Enter a string: ")

if s == "ZZZ":

break

if len(s) <= 10:

short_strings.append(s)

elif len(s) >= 11:

long_strings.append(s)

i += 1

choice = input("Enter the type of list to display [short/long] ")

if choice == "short":

if len(short_strings) == 0:

print("The list is empty.")

else:

print(short_strings)

else:

if len(long_strings) == 0:

print("The list is empty.")

else:

print(long_strings)

Step-by-step explanation:

*The code is in Python.

Initialize the count, i, short_strings, and long_strings

Create a while loop that iterates 20 times.

Inside the loop:

Ask the user to enter a string. If the string is "ZZZ", stop the loop. If the length of the string is smaller than or equal to 10, add it to the short_strings. If the length of the string is greater than or equal to 11, add it to the long_strings. Increment the value of i by 1.

When the loop is done:

Ask the user to enter the list type to display.

If the user enters "short", print the short list. Otherwise, print the long_strings. Also, if the length of the chosen string is equal to 0, print that the list is empty.

User Noctis Skytower
by
6.4k points