From Python Crash Course:
responses = {}
# Set a flag to indicate that polling is active
polling_active = True
while polling_active:
# Prompt for the person's name and response.
name = input("\nWhat is your name? ")
response = input("\nWhich mountain would you like to climb some day? ")
# Store the response in the dictionary.
responses[name] = response
# Find out if anyone else is going to take the poll.
repeat = input("Would you like to let another person respond? (yes/no)? ")
if repeat == "no":
polling_active = False
# Polling is complete. Show the results.
print("\n--- Poll Results ---")
for name, response in responses.items():
print(f"{name} would like to climb {response}. ")
print(responses)
which returns
What is your name? Roger
Which mountain would you like to climb some day? Klotz
Would you like to let another person respond? (yes/no)? no
--- Poll Results ---
Roger would like to climb Klotz.
{'Roger': 'Klotz'}
I get everything in this program, except it’s not clear to me with this code:
responses[name] = response
Can someone explain this to me? I know this results in storing the name and response in “responses”, but I don’t understand why name is surrounded in square brackets and is right next to “responses” and then there’s an equal sign for “response”. Does “=” result in a colon? Is the reason why response is after an equal sign and not in square brackets next to “responses” because the left side and equal side is just so name and response will be on opposite sides of each other?