Home > Back-end >  Why does this Python program only list the last two inputs, instead of all the inputs?
Why does this Python program only list the last two inputs, instead of all the inputs?

Time:01-11

This was intended to take any number of inputs (names and vacations) input them into a dictionary (name:[vacation list] value pairs) and display them at the end, but instead, the vacations listed only list the last two entries. Any idea why?

dreams = {}

open = True

while open:
    print('Hi and welcome to the "Dream Vacation poll"')
    more = True
    name = input('What is your name? ')
    while more:
        location = input('What is your dream vacation location or country? ')
        dreams[name] = [location]
        response_more = input('Would you like to add more locations? (yes/no) ')
        if response_more == 'no':
            more = False
        else:
            location = input('What is your other dream vacation location or country? ')
            dreams[name]  = [location]
            response_more = input('Would you like to add more locations? (yes/no) ')
            if response_more == 'no':
                more = False
    response_open = input('Are there more people taking the poll before it closes? (yes/no) ')
    if response_open == 'no':
        open = False

for name, locations in dreams.items():
    if len(locations) > 1:
        print(f"\n{name.title()}'s dream vacations are: ")
        for location in locations:
            print(location)
    else:
        print(f"\n{name.title()}'s dream vacation is: ")
        for location in locations:
            print(location)

print("\nThank you for participating in the poll!")

CodePudding user response:

The problem was in the while more loop. Each loop iteration you set the value for dreams[more] = [location] which just sets the value rather than append. Thus you need to initialize the key value pair first and then if the user wants to add something run the loop and use dreams[name].append(location).

open = True
dreams = {}
while open:
    print('Hi and welcome to the "Dream Vacation poll"')
    more = True
    name = input('What is your name? ')
    location = input('What is your dream vacation location or country? ')
    dreams[name] = [location]
    response_more = input('Would you like to add more locations? (yes/no) ')
    if response_more == 'no':
        more = False
    while more:
        location = input('What is your other dream vacation location or country? ')
        dreams[name].append(location)
        response_more = input('Would you like to add more locations? (yes/no) ')
        if response_more == 'no':
            more = False
    response_open = input('Are there more people taking the poll before it closes? (yes/no) ')
    if response_open == 'no':
        open = False

for name, locations in dreams.items():
    if len(locations) > 1:
        print(f"\n{name.title()}'s dream vacations are: ")
        for location in locations:
            print(location)
    else:
        print(f"\n{name.title()}'s dream vacation is: ")
        for location in locations:
            print(location)
    

print("\nThank you for participating in the poll!")

CodePudding user response:

Rethinking the flow of your code:

  1. Check to see if the key/name exists in your dictionary. If it does exist, append to the list in that key's entry. If it doesn't exist, then initialize the list/value
  2. Welcome the individual to the poll at the start of the program, not inside of your loop
  3. Instead of repeating the same prompt twice, just ask it once in each loop. This is more cosmetic, but it also makes your code more supportable. You only need to change your prompt verbiage in one spot.

dreams = {}

print('Hi and welcome to the "Dream Vacation poll"')
while True:

    name = input('What is your name? ')

    while True:
        
        location = input('What is your dream vacation location or country? ')
        
        #check to see if the key exists, if not then initiate the locations list. If it does, then append to the existing entry/list
        if dreams.get(name) == None:
            dreams[name] = [location]
        else:
            dreams[name].append(location)
        
        response_more = input('Would you like to add more locations? (yes/no) ')
        if response_more == 'no':
            break        
    
    response_open = input('Are there more people taking the poll before it closes? (yes/no) ')
    if response_open == 'no':
        break

for name, locations in dreams.items():
    if len(locations) > 1:
        print(f"\n{name.title()}'s dream vacations are: ")
        for location in locations:
            print(location)
    else:
        print(f"\n{name.title()}'s dream vacation is: ")
        for location in locations:
            print(location)

print("\nThank you for participating in the poll!")

CodePudding user response:

I made 2 changes. Indicated with #CHANGE 1 and #CHANGE 2

Your actual problem was initializing dreams[name] = [location] like this. When the loop starts again, your previous locations will be deleted and the current location array will have only 1 item. So start changing this code to dreams[name] = [location] .

The other change is made because of change 2. I have used = on change 2 and this means there had to be some array (could be empty) to concatenate. So on change 1, I initialized dreams[name] = [] to an empty array.

dreams = {}

open = True

while open:
    print('Hi and welcome to the "Dream Vacation poll"')
    more = True
    name = input('What is your name? ')
    dreams[name] = [] # CHANGE 1
    while more:
        location = input('What is your dream vacation location or country? ')
        dreams[name]  = [location] # CHANGE 2
        print(1, dreams)
        response_more = input('Would you like to add more locations? (yes/no) ')
        if response_more == 'no':
            more = False
        else:
            location = input('What is your other dream vacation location or country? ')
            dreams[name]  = [location]
            print(2, dreams)
            response_more = input('Would you like to add more locations? (yes/no) ')
            if response_more == 'no':
                more = False
    response_open = input('Are there more people taking the poll before it closes? (yes/no) ')
    if response_open == 'no':
        open = False

for name, locations in dreams.items():
    if len(locations) > 1:
        print(f"\n{name.title()}'s dream vacations are: ")
        for location in locations:
            print(location)
    else:
        print(f"\n{name.title()}'s dream vacation is: ")
        for location in locations:
            print(location)

print("\nThank you for participating in the poll!")
  • Related