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")
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.
+ 1
Kunevich Evgeniyy Evgenievich... Thanks man... Bt how did it work it's even different from what sololearn AI presented as answer...
+ 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.
0
Give space before and after "is"
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.
0
Didn't work guys...
0
Show your updated code.
Nobody can help if you don't provide the most updated code.
0
Didn't work as well... Per Bratthammar
0
There is no submit button... I just have to write a code that will pass all test cases...
0
If you still have trouble with that lesson, here are some pointer.
https://www.sololearn.com/discuss/3256032/?ref=app
https://www.sololearn.com/discuss/3245242/?ref=app
https://www.sololearn.com/discuss/3234991/?ref=app
https://www.sololearn.com/Discuss/3262502/?ref=app
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.
0
Nothing is working guys...
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")
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...
0
Thanks Wong Hei Ming... Its beginning to make a bit of sense... This coding stuff may be harder than I afore imagined...
0
Thanks Wong Hei Ming ... I almost wrote Wong fei Hong the first time... I like how ur name sounds