Hi guys...I'm new to python so I've no idea what I'm doing but I need to write a function that 1) asks the user how many strings they would like to input. 2)Stores the strings in a list 3) sorts the list 4) Displays the sorted list 5) Displays the shortest string 6) displays the longest string and 7) Displays the strings in the list that contain only alphabetic characters. I think i got the first 3 parts right...but that's as much as I've done.. I need to have it in the format below 2..so any help would be greatly appreciated. Thanks!

def makeList(num):
    alist=[]
    for i in range(num):
        x=input('Enter string please: ')
        alist.append(x)
    return alist

def findShortest(lst):
    shortest=[]
    p=0
    s=""
    for item in shortest:
        if(len(item)>p):
           s=item
           p=len(item)
        return s

def findLongest(lst):

def findAlpha(lst):

def main():
    num=input('Please enter the number of strings you want to input\n')
    lst=makeList(int(num))
    lst.sort()
    print(lst)
    shortest=(findShortest(lst))
    print(shortest)




main()

Edited 3 Years Ago by Dani: Formatting fixed

def findShortest(lst):
    length = len(lst)
    short = len(lst[0])
    ret = 0
    for x in range(1, length):
        if len(lst[x]) < short:
            short = lst[x]
            ret = x

    return x # return the index of the shortest sentence in the list
def findBiggest(lst):
    length = len(lst)
    biggest = len(lst[0])
    ret = 0
    for x in range(1, length):
        if len(lst[x]) > short:
            biggest = lst[x]
            ret = x

    return x # return index of the longer sentence in the list
def findAlpha(lst):
    ret = []
    length = len(lst)
    for x in (length):
       if lst[x].isalpha():
            ret.append(lst[x])

    return ret # return the list only with alphabetic sentences

The code it is not tested, but should be worked :)

Edited 6 Years Ago by Krstevski: n/a

The code it is not tested, but should be worked

You should test it because it is not working:-/

Here you have one way off do it.
This is for python 3 that i think you use.
Homework?,they may spot that you havent wirte this code(some smart shortcut in this code)
You may work on the for loop you startet with(now your code is not working)

def makeList():
    num = input('Please enter the number of strings you want to input\n')
    alist = []
    for i in range(int(num)):
        x = input('Enter string please: ')
        alist.append(x)
    return sorted(alist)

def findShortest(alist):
    min_list = min(alist, key=len)
    return (min_list)

def findLongest(alist):
    max_list = max(alist, key=len)
    return max_list

def findAlpha(alist):
    alpha_list = [item for item in alist if str(item).isalpha()]
    return alpha_list

def main():
    alist = makeList()
    print ('Sorted list is:\n{0}'.format(alist))
    print ('Shortest element is:\n{0}'.format(findShortest(alist)))
    print ('Longest element is:\n{0}'.format(findLongest(alist)))
    print ('Only alphabetic characters is:\n{0}'.format(findAlpha(alist)))

if __name__ == "__main__":
    main()

'''Out-->
Sorted list is:
['car', 'taxi', 'train5']
Shortest element is:
car
Longest element is:
train5
Only alphabetic characters is:
['car', 'taxi']
'''

Edited 6 Years Ago by snippsat: n/a

Comments
nice code

Hi guys...I'm new to python so I've no idea what I'm doing but I need to write a function that 1) asks the user how many strings they would like to input. 2)Stores the strings in a list 3) sorts the list 4) Displays the sorted list 5) Displays the shortest string 6) displays the longest string and 7) Displays the strings in the list that contain only alphabetic characters. I think i got the first 3 parts right...but that's as much as I've done.. I need to have it in the format below 2..so any help would be greatly appreciated. Thanks!

def makeList(num):
alist=[]
for i in range(num):
x=input('Enter string please: ')
alist.append(x)
return alist

def findShortest(lst):
shortest=[]
p=0
s=""
for item in shortest:
if(len(item)>p):
s=item
p=len(item)
return s

def findLongest(lst):

def findAlpha(lst):

def main():
num=input('Please enter the number of strings you want to input\n')
lst=makeList(int(num))
lst.sort()
print(lst)
shortest=(findShortest(lst))
print(shortest)


main()

You should really use code tags.

def makeList(num):
    alist=[]
    for i in range(num):
        x=input('Enter string please: ')
        alist.append(x)
    return alist

def findShortest(lst):
    shortest=[]
    p=0
    s=""
    for item in shortest:
        if(len(item)>p):
           s=item
           p=len(item)
        return s
   
def findLongest(lst):

def findAlpha(lst):

def main():
    num=input('Please enter the number of strings you want to input\n')
    lst=makeList(int(num))
    lst.sort()
    print(lst)
    shortest=(findShortest(lst))
    print(shortest)

This looks better.

This part:

x=input('Enter string please: ')

should be

x= raw_input('Enter string please: ')

This part:

x=input('Enter string please: ')

should be

x= raw_input('Enter string please: ')

In Python 3.0 input() is the same as raw_input().

In Python 3.0 input() is the same as raw_input().

but raw_input is compatible to both version 3 and 2.x

def get_list():
	return [raw_input("Please enter a string: ") for i in range(int(raw_input("Please enter the number of strings you want to input\n")))]

def shortest(lst):
	return min(lst, key = len)

def longest(lst):
	return max(lst, key = len)

def alpha(lst):
	return [e for e in lst if e.isalpha()]

if __name__ == "__main__":
	lst = get_list()
	lst.sort()
	print ", ".join(lst)
	print shortest(lst)
	print longest(lst)
	print ", ".join(alpha(lst))

array_list = ["name123","name12244","name123456"]
print max(array_list , key=len)

This will print the longest string

Just as an exercise:

'''
use very basic Python syntax to form a list of strings and process
'''

import sys

# make string input work with Python2 or Python3
if sys.version_info[0] < 3:
    input = raw_input

def makeList(num):
    '''
    store the strings input in a list
    '''
    alist=[]
    for i in range(num):
        x = input(str(i+1) + ') Enter string please: ')
        alist.append(x)
    return alist

def findShortest(lst):
    shortest=[]
    p = 100
    s = ""
    for item in lst:
        # less than
        if len(item) < p:
            s = item
            p = len(item)
    return s

def findLongest(lst):
    longest=[]
    p = 0
    s = ""
    for item in lst:
        # larger than
        if len(item) > p:
            s = item
            p = len(item)
    return s

def printAlpha(lst):
    '''
    display the strings in the list that contain only
    alphabetic characters 
    '''
    for s in lst:
        flag = True
        # look at the characters in string s
        for c in s:
            if not c.isalpha():
                flag = False
        if flag == True:
            print(s)

    pass

def main():
    num=input('Please enter the number of strings you want to input: ')
    lst = makeList(int(num))
    print("Sorted list:")
    lst.sort()
    print(lst)
    print("Shortest item:")
    shortest = findShortest(lst)
    print(shortest)
    longest = findLongest(lst)
    print("Longest item:")
    print(longest)
    print("Alphabetic strings are:")    
    printAlpha(lst)


main()
This question has already been answered. Start a new discussion instead.