+ 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.
11 odpowiedzi
+ 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!'))
+ 11
Note: This is dictionary not tuple.
+ 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]))
+ 7
print(Contact["john"])
or
print(Contact.get("john"))
+ 7
Thank you so much Quantum
+ 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.
+ 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!'))
+ 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
+ 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.
+ 1
Can someone explain tuples exercise for me please