I'am a newbee in python and coding, and I really don't understand why my program isn't working correctly. The expected effect is "name wants to go to [destinations]" while my code doesn't distinguish different "{names : [destinations]}" pairs.
Here's my code, thanks for your attention here :)
response = {}
destinations = []
ptname = 'name pls.'
prompt = 'input some places in the world, input next to go to the next user'
active = True
unfinished = True
while active:
unfinished = True
name = input(ptname)
while unfinished:
destination = input(prompt)
if destination != 'next':
destinations.append(destination)
print(destination + ' has been added to your list.')
response[name] = destinations
elif destination == 'next':
unfinished = False
go_on = input('wish to continue? y/n')
if go_on == 'n':
active = False
print(response)
for name, destinations in response.items():
print(name + ' wants to go to ')
for destination in destinations:
print(destination)
you have to reset (actually create a new reference) for destinations
in the same outer loop where you set name
or you're be reusing the same list for all names.
name = input(ptname)
destinations = [] # create new reference for the list
while unfinished:
...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.