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

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
Camélia Morning'star
Camélia Morning'star - avatar
8 Answers
+ 5
# 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
Per Bratthammar - avatar
+ 5
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
Lothar - avatar
+ 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
I am offline
I am offline - avatar
+ 1
Per Bratthammar Has given the best code Camélia Morning'star you can mark it
11th Sep 2022, 5:08 PM
I am offline
I am offline - avatar
+ 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
Shahram - avatar
+ 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
James Nesbitt Jr - avatar
+ 1
track whether the name is found in the list of contacts. 4. We use a `for` loop to iterate through the list of contacts. Inside the loop, we compare the name of each contact (the first element of the tuple, `contact[0]`) with the `name_to_find` input provided by the user. 5. If we find a match, we print the name and age of the contact using an f-string (`f"{name_to_find} is {contact[1]}"`), and we set the `found` flag to `True` to indicate that we found the name. 6. We use a conditional statement (`if not found`) to check whether the `found` flag is still `False` after the loop. If it is, it means the name was not found in the list, so we print "Not Found." This script allows the user to input a name, searches for it in the list of contacts, and prints the corresponding age if found, or "Not Found" if not found..
29th Sep 2023, 3:53 PM
James Nesbitt Jr
James Nesbitt Jr - avatar
0
you gave an outstanding explanation, at first it was tricky on my side then it fell it place, i dont think i will forget the concept, thank you
30th Jan 2024, 8:33 AM
Mugabi David
Mugabi David - avatar