I'm currently making a program that saves the longest and shortest words in a sentence inside a list but i cant seem to append multiple words in a list as well as enter multiple sentence and only stop after a specific text or number is inputed. Python gui still confuses me alot.

class Find:
        def long(self):
                inputsentence = raw_input("Write a sentence: ").split()
                long = []
                for word in inputsentence:
                        if len(word) == len(max(inputsentence, key=len)):
                                long.append(word)
                                print long
        def short(self):
                inputsentence = raw_input("Write a sentence: ").split()
                short = []
                for word in inputsentence:
                        if len(word) == len(min(inputsentence, key=len)):
                                short.append(word)
                                print short

print "Pick shortest or longest"
print "[L]ongest"
print "[S]hortest"

f=Find()
while 1:
        choice = raw_input('Enter choice: ').lower()
        if choice == "l":
                f.long()
        elif choice == "s":
                f.short()
        else:
                print "Invalid choice!"

Recommended Answers

All 5 Replies

You are not using a Python GUI.

Do you mean a multiline sentence?

Otherwise put your input in a while loop with a break condition.

Something like this:

def sentence_input():
    print("Enter your sentences (qq to quit):")
    sentence_list = []
    while True:
        sentence = raw_input("Sentence: ")
        if sentence == "qq":
            break
        sentence_list.extend(sentence.split())
    return sentence_list

print(sentence_input())

Now your code might look like this (4 spaces are customary for indentations):

class Find:

    def sentence_input(self):
        print("Enter your sentences (qq to quit):")
        sentence_list = []
        while True:
            sentence = raw_input("Sentence: ")
            if sentence == "qq":
                break
            sentence_list.extend(sentence.split())
        return sentence_list

    def long(self):
        inputsentence = self.sentence_input()
        long = []
        for word in inputsentence:
            if len(word) == len(max(inputsentence, key=len)):
                long.append(word)
                print long

    def short(self):
        inputsentence = self.sentence_input()
        short = []
        for word in inputsentence:
            if len(word) == len(min(inputsentence, key=len)):
                short.append(word)
                print short

print "Pick shortest or longest"
print "[L]ongest"
print "[S]hortest"

f = Find()
while 1:
    choice = raw_input('Enter choice (L or S): ').lower()
    if choice == "l":
        f.long()
    elif choice == "s":
        f.short()
    else:
        print "Invalid choice!"

thank you this got closer but it still doesn't append the longest word per sentence to 1 list

I don't have that problem, can you illustrate an example?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.