Tuples | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 6

Tuples

I want to create a contact book In which I can type someone's name and get his number : My code attempt : Contact = {'john': '78987654', 'James: '7890657'} If John I contact: print('78987654') The thing I'm not able to understand is how to I input John and get output as contact number.

14th Apr 2021, 2:27 PM
Veena Tirmal
Veena Tirmal - avatar
11 Answers
+ 14
Veena Tirmal , in general, it is better using Contact.get(...) instead of Contact[...]. using get(...) allows a message that will be given when name is not found. see the code below: Contact = {'john': '78987654','James': '7890657'} name = input() print(Contact[name]) # this creates a key error (program crashes) when name is not found e.g input is 'jon' # to avoid this you could check: if name in Contact:... but better to use get() print(Contact.get(name,'Name not found!'))
14th Apr 2021, 3:13 PM
Lothar
Lothar - avatar
+ 11
Note: This is dictionary not tuple.
14th Apr 2021, 2:41 PM
The future is now thanks to science
The future is now thanks to science - avatar
+ 8
I did it finally after 3 hours🥲🥲 contacts = [ ('James', 42), ('Amy', 24), ('John', 31), ('Amanda', 63), ('Bob', 18) ] name = input() for v in contacts: if v[0]==name: print(v[0] + " is " + str(v[1]))
14th Apr 2021, 3:37 PM
Veena Tirmal
Veena Tirmal - avatar
+ 7
print(Contact["john"]) or print(Contact.get("john"))
14th Apr 2021, 2:35 PM
Jan
Jan - avatar
+ 7
Thank you so much Quantum
14th Apr 2021, 2:37 PM
Veena Tirmal
Veena Tirmal - avatar
+ 4
ok u can put this down in the bottom of ur code after ommiting the 2 last lines. name = input("Enter Contact Name: ") if name in Contact: print(Contact[name]) else: print("[ ! ] Contact name not found.") explaination: here we go u can store the keys of a dictionary in var and u can also use it like tht : >>> print(DICT[KY]) this will print the value of the KY var in the DICT dictionary. and then u can use the statement to avoid the errors like checking if the KY key is exists in the DICT dictionary and so on.
15th Apr 2021, 9:35 PM
Imrane Aabbou
Imrane Aabbou - avatar
16th Apr 2021, 12:45 AM
Mahad Ahmed
Mahad Ahmed - avatar
+ 3
Veena Tirmal , in general, it is better using Contact.get(...) instead of Contact[...]. using get(...) allows a message that will be given when name is not found. see the code below: Contact = {'john': '78987654','James': '7890657'} name = input() print(Contact[name]) # this creates a key error (program crashes) when name is not found e.g input is 'jon' # to avoid this you could check: if name in Contact:... but better to use get() print(Contact.get(name,'Name not found!'))
16th Apr 2021, 6:39 AM
M.Thasnchzayan
M.Thasnchzayan - avatar
+ 1
print (contact.get (input (),"name not found")) explanation: we get the input and immediately pass it to the get method with default value as "name not found!" which will be returned if the name isnt found, then we print whatever the method returned this way, if the name isnt there, we will print "name not found!" else we will print the contact's number
16th Apr 2021, 6:37 AM
Bot
Bot - avatar
+ 1
# Define the contacts dictionary contacts = {'John': '78987654', 'James': '7890657'} # Get the contact number for a given name name = input("Enter a name: ") if name in contacts: print(contacts[name]) else: print("Contact not found") In this code, the contacts dictionary stores the names and phone numbers of your contacts. To retrieve the phone number for a given name, the code prompts the user to enter a name using the input() function. It then checks if the name exists in the contacts dictionary using the in keyword. If the name is found, the corresponding phone number is retrieved using the dictionary key as an index, and it's printed to the console. Otherwise, the code prints an error message indicating that the contact was not found. For example, if you enter "John" as the name, the code will retrieve the phone number 78987654 from the contacts dictionary and print it to the console.
4th Mar 2023, 10:45 PM
Hasan Kuluk
+ 1
Can someone explain tuples exercise for me please
12th Sep 2023, 6:50 AM
Siaka Bamba
Siaka Bamba - avatar