0

I am trying to input [1] and enter a new word to append the existing words at list '''words''' in shuffle pattern.

import random

words = ['apple', 'banana', 'orange', 'coconut', 'strawberry', 'lime']
old_word = []

while True:
    choice = int(input('Press [1] to continue [2] to exit: '))
    if choice == 2:
        break
    
    elif choice == 1:
        new_word = input('Enter a new word: ')
        old_word.append(new_word)
        words.append(old_word)
        if new_word in words:
            random.shuffle(words)
            print(words)

e.g input 1 Enter a new word: lemon

output 'orange', 'banana', 'lime', 'apple', 'lemon','coconut', 'strawberry'

  • 1
    ```int(input('Press [1] to continue [2] to exit: '))```. Your condition is never true because ```input``` returns a string, but you compare it with an integer –  Jul 29 '21 at 17:37
  • In the future, try to focus your question around the specific technical problem you encountered, not the program you were trying to write when you encountered it. The linked duplicate is a good example of doing that well (the code given is a proper [mre], and the title is narrowly focused on the technical issue that caused the question to be asked). – Charles Duffy Jul 29 '21 at 18:10

1 Answers1

0

Correction:

  • It should be int(input('Press [1] to continue [2] to exit: ')). Your condition is never true because input returns a string, but you compare it with an integer.

Also, is old_word list for storing the new words? If yes, then you just need to add the element. Don't add the whole list to words

import random

words = ['apple', 'banana', 'orange', 'coconut', 'strawberry', 'lime']
old_word = []

while True:
    choice = int(input('Press [1] to continue [2] to exit: '))
    if choice == 2:
        break
    
    elif choice == 1:
        new_word = input('Enter a new word: ')
        old_word.append(new_word)
        words.append(new_word)
        if new_word in words:
            random.shuffle(words)
            print(words)
  • doesn't work exacty. when i enter **1** and enter a new word it does not return the new word nor the words from the existing list @Sujay –  Jul 29 '21 at 18:00