Question #70281

you will write a program that will store sentences in a list and get practice with working with lists
and strings. First, ask how many sentences the user wants to store. You can assume that you will get an
integer, but if it is non-positive, default to the value of 5. Next, get the user input (i.e. the sentences) and
store them in your list in all lower-case letters. You can assume that the user will enter sentences without
the . at the end.
Next, you will loop the same number of times as the number of sentences in your list and in each iteration
you will display the following menu:
print ("Enter 1 to see a sentence")
print ("Enter 2 to see the whole list")
print ("Enter 3 to change a sentence")
print ("Enter 4 to switch words")
print ("Enter 5 to count letters")
You can assume that the user will enter an integer. Below is what you should do depending on the choice.
1

Expert's answer

2017-10-02T12:54:07-0400
# python program
import string
def main():
    while 1:
        try:
            n = int(input('How many sentences you want to store:'))
            if n >= 0:
                break
            else:
                n = 5
                break
        except ValueError:
            print('Please enter a valid number.')
    sentences = []
    print('Enter the sentences:')
    for i in range(n):
        sentences.append(str(input()).lower())
    for i in range(n):
        print("Enter 1 to see a sentence")
        print("Enter 2 to see the whole list")
        print("Enter 3 to change a sentence")
        print("Enter 4 to switch words")
        print("Enter 5 to count letters")
        try:
            option = int(input())
            if option == 1:
                print(sentences[i])
            elif option == 2:
                print('\n').join(sentences)
            elif option == 3:
                new = input('Enter a new sentence:\n')
                sentences[i] = new
            elif option == 4:
                sentences[i] = sentences[i][::-1]
                print(sentences[i])
            elif option == 5:
                _count = sum(list(map(lambda x: len([char for char in x if char not in string.punctuation+'']), sentences)))
                print('There are {} letters.'.format(_count))
        except ValueError:
            print('Enter one of the option')
if __name__ == '__main__':


Answer provided by www.AssignmentExpert.com

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!
LATEST TUTORIALS
APPROVED BY CLIENTS