Skip to content

Programming exercise: Story (SUGGESTED FIX) #55

Description

@valentin-esmeralda

I found when I used this code the program accepted it (code 1 below). The output to code 1 is seen as:

(base) Valentín Python_Programming_MOOC $ python3 construction.py
Please type in a word: hello hi
Please type in a word: sup sup
Please type in a word:

Notice that if you input to nonidentical words in the beginning, and then two identical the program still expects it. I think this should also be tested rather than just "hello" "world world" but something like "hi hello" "world world". I fixed this with code 2.

CODE 1

words = ''
word_list = []
word_list_dup = []

while True: 
    word = input("Please type in a word: ")
    if word != 'end':

        if " " in word:
            # print('There is a space:')
            word_list_dup.append(word.split())
            # print(f'words with space: {word_list_dup}')
            word_list.extend(word_list_dup[0][::])
            # print(" ".join(word_list))
            # print('printing here 1')

            if word_list[-1] == word_list[-2]:
                # print('printing here 1')
                print(" ".join(word_list[:-1]))
                break

        elif " " not in word:
            words += word + ' '
            word_list.append(word)
            # print('printing here 2')
            # print(" ".join(word_list))
        
            if len(word_list) >= 2:
                if word_list[-1] == word_list[-2]:
                    # print('printing here 3')
                    print(" ".join(word_list[:-1]))
                    break
                else:
                    # print('printing here 4')
                    # print(" ".join(word_list))
                    continue

    elif word == 'end':
        print(" ".join(word_list))
        # print('printing here end')
        break

CODE 2


words = ''
word_list = []
word_list_dup = []

while True: 
    word = input("Please type in a word: ")
    if word != 'end':

        if " " in word:
            new_words = word.split()
            word_list.extend(new_words)
            # print('printing here 1')
            # print(f'word list: {word_list}')
            # print(f'length of word list: {len(word_list)}')

            if len(word_list) >= 2: # new line here
                if word_list[-1] == word_list[-2]:
                    # print('printing here 2')
                    print(" ".join(word_list[:-1]))
                    break

        elif " " not in word:
            words += word + ' '
            word_list.append(word)
            # print('printing here 3')
            # print(" ".join(word_list))
        
            if len(word_list) >= 2:
                if word_list[-1] == word_list[-2]:
                    # print('printing here 4')
                    print(" ".join(word_list[:-1]))
                    break
                else:
                    # print('printing here 5')
                    # print(" ".join(word_list))
                    continue

    elif word == 'end':
        print(" ".join(word_list))
        # print('printing here end')
        break

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions