+ 4

# Tuples problem in python

I just need that you help me in solving this problem You are given a list of contacts, where each contact is represented by a tuple, with the name and age of the contact. Complete the program to get a string as input, search for the name in the list of contacts and output the age of the contact in the format presented below: Sample Input John Sample Output John is 31 If the contact is not found, the program should output "Not Found" This is the code contacts = [ ('James', 42), ('Amy', 24), ('John', 31), ('Amanda', 63), ('Bob', 18) ] name=input() for x in contacts: if name in x: print(str(x[0])+" is "+ str(x[1])) break elif name not in contacts: print("Not found") else: break Please hello me

11th Sep 2022, 4:09 PM
+ 6
# Hi! Snehil Pandey # If you do as you do, you will break the for loop when it comes to the elif. Try this instead: contacts = [ ('James', 42), ('Amy', 24), ('John', 31), ('Amanda', 63), ('Bob', 18) ] name = input() for x in contacts: if name == x[0]: print(x[0] + " is " + str(x[1])) break else: print("Not found")
11th Sep 2022, 4:47 PM
Per Bratthammar
+ 6
a slightly improvement of the presented codes, that gives a better readability. the tuple will be unpacked to 2 variables in the for loop header: ... search = input() for name, age in contacts: if name == search: print(f'{name} is {age}') break else: print('Not Found')
11th Sep 2022, 6:55 PM
Lothar
+ 4
https://code.sololearn.com/cend34rjXgFQ/?ref=app just remove else block and add its statement in elif block
11th Sep 2022, 4:14 PM
observer
+ 1
Per Bratthammar Has given the best code CamĂ©lia Morning'star you can mark it
11th Sep 2022, 5:08 PM
observer
+ 1
In the line where you get user input add capitalize() method like this: name=input().capitalize() with this if user enters a name in lower-case your code will not return "Not found". finally delete your elif condition and put print("Not found") in else before break. this way your code runs as expected and is cleaner.
13th Sep 2022, 9:40 AM
Shahram
+ 1
# List of contacts contacts = [ ('James', 42), ('Amy', 24), ('John', 31), ('Amanda', 63), ('Bob', 18) ] # Input from the user name_to_find = input() # Initialize a flag to track whether the name is found found = False # Iterate through the list of contacts to search for the name for contact in contacts: if contact[0] == name_to_find: # If the name is found, print the name and age print(f"{name_to_find} is {contact[1]}") found = True break # If the name is not found, print "Not Found" if not found: print("Not Found") Now, let's break down the script step by step: 1. We start by defining the list of contacts, where each contact is represented by a tuple containing their name and age. 2. We use the `input()` function to get the name that the user wants to search for. The user is prompted to enter a name, and the input is stored in the `name_to_find` variable. 3. We initialize a boolean variable `found` to `False`. This variable will help us tra
29th Sep 2023, 3:53 PM
James Nesbitt Jr
+ 1