0

TUPLES UNDER PYTHON INTERMIDATE ( pls help )

contacts = [ ('James', 42), ('Amy', 24), ('John', 31), ('Amanda', 63), ('Bob', 18) ] name = input() for y in contacts : if name in y: print(str(y[0]) + "is" + str(y[1]) ) break else: print("Not Found")

24th Jan 2024, 10:21 AM
CHRISTIAN
CHRISTIAN - avatar
17 Answers
+ 2
# Hi, CHRISTIAN ! # You try something like this: contacts = [('James', 42), ('Amy', 24), ('John', 31), ('Amanda', 63), ('Bob', 18)] search_for = input().lower().title() for name, age in contacts: if name == search_for: print(f"{name} is {age} years.") break else: print("Not Found") # Just modify the code to fit your needs.
24th Jan 2024, 12:49 PM
Per Bratthammar
Per Bratthammar - avatar
+ 2
CHRISTIAN ! You’ve must run the code as a Python code in Playgeound! Copy and past my code, run it, write ”amanda”, press Submit button and the answare will be: ”Amanda is 63 years”.
24th Jan 2024, 5:03 PM
Per Bratthammar
Per Bratthammar - avatar
+ 1
Kunevich Evgeniyy Evgenievich... Thanks man... Bt how did it work it's even different from what sololearn AI presented as answer...
25th Jan 2024, 12:17 PM
CHRISTIAN
CHRISTIAN - avatar
+ 1
CHRISTIAN You can mention someone by entering @ and then select the user. The writing style in Intermediate is poor compare to Introduction IMO, and there are post complaining that. If you have difficulity about coding, open your search engine and search for more info / tutorial online. Also, practicing is very important.
25th Jan 2024, 1:16 PM
Wong Hei Ming
Wong Hei Ming - avatar
0
Give space before and after "is"
24th Jan 2024, 10:35 AM
A͢J
A͢J - avatar
0
Else has a wrong indentation. However, fixing it cause other problem. You can create a status variable which allow print Not Found if there is no matching. Alternatively, defind a function which take the input as function parameter, and print the result.
24th Jan 2024, 10:42 AM
Wong Hei Ming
Wong Hei Ming - avatar
0
Didn't work guys...
24th Jan 2024, 11:26 AM
CHRISTIAN
CHRISTIAN - avatar
0
Show your updated code. Nobody can help if you don't provide the most updated code.
24th Jan 2024, 12:36 PM
Wong Hei Ming
Wong Hei Ming - avatar
0
Didn't work as well... Per Bratthammar
24th Jan 2024, 4:21 PM
CHRISTIAN
CHRISTIAN - avatar
0
There is no submit button... I just have to write a code that will pass all test cases...
24th Jan 2024, 5:29 PM
CHRISTIAN
CHRISTIAN - avatar
0
CHRISTIAN Your code is right, you just have to give single space before and after "is". I don't know why you say not working.
25th Jan 2024, 4:12 AM
A͢J
A͢J - avatar
0
Nothing is working guys...
25th Jan 2024, 6:32 AM
CHRISTIAN
CHRISTIAN - avatar
0
contacts = [ ('James', 42), ('Amy', 24), ('John', 31), ('Amanda', 63), ('Bob', 18) ] user_in=input() temp = None for i in contacts: if user_in in i: temp=i[1] if temp: print(f"{user_in.capitalize()} is {temp}") else: print("Not Found")
25th Jan 2024, 12:02 PM
Kunevich Evgeniyy Evgenievich
Kunevich Evgeniyy Evgenievich - avatar
0
CHRISTIAN Kunevich Evgeniyy Evgenievich's method is what I mentioned at the beginning, a status variable. In this code, "temp" is the status variable and set to None. In the for loop, it check for each tuple (i). If given input is inside the tuple it assign the age to temp. It keep checking until the for loop end. After the loop, it check if temp is True or False. It is like calling bool(temp). To make thing simple, None, False, 0, '' or other empty sequences are consider falsely. More detail you can consult the python documentation. https://docs.python.org/3/library/functions.html#bool Therefore, if a new new value is assigned to temp it prints the sentence, otherwise it prints Not Found. print(f"{user_in.capitalize()} is {temp}") contains something not covered in SoloLearn, a f-string method. You can consult this code for more detail. https://sololearn.com/compiler-playground/c81oGO8Ygckc/?ref=app By the way, capitalize() is not necessary in here...
25th Jan 2024, 1:03 PM
Wong Hei Ming
Wong Hei Ming - avatar
0
Thanks Wong Hei Ming... Its beginning to make a bit of sense... This coding stuff may be harder than I afore imagined...
25th Jan 2024, 1:08 PM
CHRISTIAN
CHRISTIAN - avatar
0
Thanks Wong Hei Ming ... I almost wrote Wong fei Hong the first time... I like how ur name sounds
25th Jan 2024, 1:20 PM
CHRISTIAN
CHRISTIAN - avatar