+ 2

Python bug: Spot the bug

Please guys I am trying to create a code which asks for name as input and outputs name is age but there’s something wrong with this code can you help spot it. contacts = ( ('James,42'), ('Amy,24'), ('John,31'), ('Amanda,63'), ('Bob,18'), ) for contact in contacts: print(contact) print(name,'is',age)

29th Jan 2024, 11:07 AM
Fikayo Abayomi
Fikayo Abayomi - avatar
10 Respostas
+ 8
Oluwafikayo Oyeleke , (can you please remove the empty lines in your post?) if I'm not mistaken, the input is given like this as a list of tuples. please check this: contacts = [ ('James', 42), ('Amy', 24), ('John', 31), ('Amanda', 63), ('Bob', 18) ] > first take an input from the user and store it. > iterate through this given list will give us one tuple at a time in the loop variable. with this tuple you can check if it contains the name to search for. we can use the *in* operator. > we can access the 2 required elements of the tuple by using the index notation.
29th Jan 2024, 11:43 AM
Lothar
Lothar - avatar
+ 8
Bob_Li , Wong Hei Ming , Jan , *here some thoughts from me about using the search in sololearn*: you might be all right from your point of view, but the askers are mostly beginners or people with not a lot of experience. so if they start searching for similar posts from the past, they will get a wall of these former posts as an answer. the issue with all of these answers is, that there are some of them that spotted the correct facts, but there are also some that are only telling half of the truth, others are completely irrelevant or misleading. do you believe that people will read all of the answers with all pro and cons? do you believe that the asker is just picking the correct one? please keep in mind, that there is no *measure* in sololearn about where inbetween true and false a particular answer from a former post is.
29th Jan 2024, 8:55 PM
Lothar
Lothar - avatar
+ 8
Jan , i don't think that using a dictionary should be the preferred way, because the tutorial is about tuples / tuple unpacking.
29th Jan 2024, 9:01 PM
Lothar
Lothar - avatar
+ 2
The amount of times this question had been asked makes me think the lessons did not teach the required skills needed to solve the test.
29th Jan 2024, 12:02 PM
Bob_Li
Bob_Li - avatar
+ 2
Bob_Li Indeed. This question almost appear almost every second week. It is probably due to lack of hands-on experience. The intro is relatively easy compare to intermediate, which allow learners to graduate without much trouble. Come to think about this, questions about intro are the very beginning lessons, such as input and variable. There are a few about loops, but I can't remember there are questions about if...elif...else. If SL can add a pointer to relative code coach at the end of each lesson, learners can gain hands-on experience, writing the code from scratch.
29th Jan 2024, 1:07 PM
Wong Hei Ming
Wong Hei Ming - avatar
+ 1
contacts = dict(contacts) ...then you have a dictionary to work with instead that makes it more convenient...
29th Jan 2024, 12:41 PM
Jan
Jan - avatar
+ 1
Wong Hei Ming But your solution will not prevent repeating posts about the same issue, because new users probably don't think about going to older posts or make a search first. I think the solution should be, that a user gets redirected to an earlier post about the same issue when trying to post about an issue that already exists.
29th Jan 2024, 4:13 PM
Jan
Jan - avatar
0
Jan, I think this question is different from the others. Others ar struggling how to access the tuple elements, or how to print Not Found when there is no match. This one not only lacking the mentioned elements, it also lacking the decision-making process. It makes other solutions may not work for OP.
30th Jan 2024, 4:22 AM
Wong Hei Ming
Wong Hei Ming - avatar
0
Wong Hei Ming You can either iterate or convert it to a dictionary, and there is nothing in the description of the task saying that it's not allowed to convert it to a dictionary, and since the tuples are key/value pairs, then it's more convenient to use a dictionary instead. I have actually made that task myself long time ago.
30th Jan 2024, 6:36 AM
Jan
Jan - avatar
0
It's not forbidden to use Google either to solve a task!
30th Jan 2024, 7:09 AM
Jan
Jan - avatar