Home > Net >  python List will only use last user input
python List will only use last user input

Time:10-18

When running this code and putting in different inputs it only takes the last user input and splits up the word into characters

def myFallMadLib():
    
    list = []

    while True:
        list = input("Enter a noun:")
        list = input("Enter a color:")
        list = input ("Enter a second color:")
        list = input ("Enter a adverb:")
        list = input ("Enter a noun:")
        list = input ("Enter an adjective:")
        list = input ("Enter a noun:")
        list = input ("Enter a noun:")
        list = input ("Enter a verb:")
        list = input ("Enter an adjective:")
        list = input ("Enter a noun:")
        list = input ("Enter an adjective:")
        list = input ("Enter a noun:")
        quit = input ("Enter q to quit:")
        if (quit) == "q":
         break

    #list.append(data)
    
    for words in list:
        print("Fall has arrived with a chill in the ", words)
        print("The leaves are turining ", words, " and ", words)
        print("Night time comes quicker, which usually means ", words, words)
        print("But today was a ", words, " day.")
        print("My ", words, " went to the nearby ", words, " where we went on a hay ", words, " to a big ", words, " field.")
        print("and I even got to pick my own ", words)
        print("When we get home we'll bake a(n) ", words, " pie and have some ", words, " by the fire.")

   
myFallMadLib()

CodePudding user response:

using = creates a new variable every time

You should use append() method

An example with your while loop would be

list_values = []

while True:
    list_values.append(input("Enter a noun:"))

CodePudding user response:

As I can see you want different input message as well as output words from list, in that case :

  1. You should't use loops
  2. You should't use = as it creates a new variable

Here correction of your code

list = []
list.append(input("Enter a noun: "))
list.append(input("Enter a color: "))
list.append(input("Enter a second color: "))
list.append(input("Enter a adverb: "))
list.append(input("Enter a noun: "))
list.append(input("Enter an adjective: "))
list.append(input("Enter a noun: "))
list.append(input("Enter a noun: "))
list.append(input("Enter a verb: "))
list.append(input("Enter an adjective: "))
list.append(input("Enter a noun: "))
list.append(input("Enter an adjective: "))
list.append(input("Enter a noun: "))

print("Fall has arrived with a chill in the ", list[0])
print("The leaves are turning ", list[1], " and ", list[2])
print("Night time comes quicker, which usually means ", list[3], list[4])
print("But today was a ", list[5], " day.")
print("My ", list[6], " went to the nearby ", list[7], " where we went on a hay ", list[8], " to a big ", list[9], " field.")
print("and I even got to pick my own ", list[10])
print("When we get home we'll bake a(n) ", list[11], " pie and have some ", list[12], " by the fire.")

If you want loop then you can try something like this

list = []
while True:
   word = input("Enter a word: ")
   if word == "q":
       break
   else:
       list.append(word)
  • Related