107k views
3 votes
Create an array of strings. Let the user decide how big this array is, but it must have at least 1 element. Prompt them until them give a valid size. Prompt the user to populate the array with strings Display the longest and shortest string Input an array size for you words array: 5 Now please enter 5 words Input a word: apples Input a word: eat Input a word: banana Input a word: spectacular Input a word: no The longest word is : spectacular The shortest word is : no

1 Answer

0 votes

Answer:

lst = []

n = int(input("Input an array size for you words array: "))

print("Now please enter " + str(n) + " words")

max = 0

min = 1000

index_min = 0

index_max = 0

for i in range(n):

s = input("Input a word: ")

lst.append(s)

if len(s) >= max:

max = len(s)

index_max = i

if len(s) <= min:

min = len(s)

index_min = i

print("The longest word is :" + lst[index_max])

print("The shortest word is :" + lst[index_min])

Step-by-step explanation:

Create an empty list, lst

Get the size from the user

Create a for loop that iterates "size" times

Inside the loop, get the strings from the user and put them in the lst. Find the longest and shortest strings and their indices using if structure.

When the loop is done, print the longest and shortest

User Mohamed Elrashid
by
4.4k points