responses = {} polling_active = True while polling_active: name = input("\nWhat is your name? ") response = input("Which mountain would you like to climb someday? ") responses[name] = response # <<< I know this line stores the response in the dictionary, is it store the name as a key and response as a value?I am confused by it repeat = input("Would you like to let another person respond? (yes/ no) ") if repeat == 'no': polling_active = False print("\n--- Poll Results ---") for name, response in responses.items(): print(f"{name} would like to climb {response}.")I know this line stores the response in the dictionary, is it stores the name as a key and response as a value?I am confused by it.
I am confused with the key and value thing
I am confused with the key and value thing
|
Feb-22-2023, 06:50 AM
Yes, it does - each
name is key and corresponding response - value in the dict
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link Create MCV example Debug small programs
Feb-22-2023, 04:37 PM
Yes. Keys are to dicts what indexes (indices) are to lists and tuples.
Feb-22-2023, 10:43 PM
Lista and arrays are indexed using an integer. Integer indexing is indexing that uses integer values starting at 0 and going up to the number of elments in the list minus 1. Elements appear in the order of the ordinal value of the integer. list_[0] is the first element, list_[1] the second and so on.
Dictionary are indexed using a key. The key can be any hashable type (essentially any non-mutable type). Often these are str objects, but you can use integers, floats, sets, tuples, object methods, functions, classes, ... Order of elements in a dictionary is not important. element[2] may not be "after" element[1] or "before" element[3]. When you print a dictionary, or list the dictionary keys()/values()/items() the order is the order the elements were added to the dictionary. |
|
Possibly Related Threads… | |||||
Thread | Author | Replies | Views | Last Post | |
String int confused | janeik | 7 | 2,593 |
Aug-02-2023, 01:26 AM Last Post: deanhystad |
|
Need help i just started the whole thing | gabriel789 | 16 | 5,978 |
Sep-12-2022, 08:04 PM Last Post: snippsat |
|
i making a terminal sign up website thing | Kenrichppython | 1 | 2,384 |
Nov-04-2021, 03:57 AM Last Post: bowlofred |
|
Pandas confused | DPaul | 6 | 3,816 |
Sep-19-2021, 06:45 AM Last Post: DPaul |
|
is and '==' i'm confused | hshivaraj | 6 | 3,740 |
Sep-15-2021, 09:45 AM Last Post: snippsat |
|
use thing before self | sylvanas | 3 | 3,252 |
Jul-21-2021, 04:54 PM Last Post: sylvanas |
|
Confused with 'flags' | tester_V | 10 | 6,858 |
Apr-12-2021, 03:03 AM Last Post: tester_V |
|
Not able to make a specific thing pause in pygame | cooImanreebro | 4 | 4,607 |
Dec-13-2020, 10:34 PM Last Post: cooImanreebro |
|
Simple Tic Tac Toe but I'm confused | Izith | 1 | 3,469 |
Sep-26-2020, 04:42 PM Last Post: Larz60+ |
|
I am really confused with this error. | Runar | 3 | 4,048 |
Sep-14-2020, 09:27 AM Last Post: buran |
Users browsing this thread: 1 Guest(s)