Insight on what i did wrong here? Thank you for replying in advance
common = 
for i in s1:
if (s1[i]) == (s2[i]):
What I expected to happen:
[’ ', ‘h’, ‘a’, ‘v’, ‘e’, ‘b’, ‘n’, ‘u’, ‘s’, ‘i’, ‘r’, ‘t’, ‘d’, ‘y’, ‘o’, ‘m’, ‘p’, ‘.’]
What actually happened:
TypeErrorTraceback (most recent call last)
<ipython-input-1-2847a4e2523f> in <module>()
4 common = 
5 for i in s1:
----> 6 if (s1[i]) == (s2[i]):
TypeError: string indices must be integers
In here the value in
i is a single character. But you are making it a index which is not possible. You can use something for indexing when it is an integer.
I would suggest you to use the below website in order to visualize what is happening in your code. And also check the solution code in it to get a better understanding.
If you find this solution helpful kindly mark it as a solution so if someone has similar query then they can get to know the answer.
i can check the solution given but that will be just for checking sake. I will not come to know how to think by just checking solution.
The website I have mentioned allows you to have a visual clarity regarding the code. Also, I have explained the thing in your code in the first line.
First, when you use
i as index it won’t run ahead as if you want to use index means numbers, integers. That is why you are getting error of string indices must be integers. And also you have used
== which checks one character at that instance only. It would always return a void list. While using
in operator returns True if a sequence with the specified value is present in the object.
In here we have to check whether the single character is present in the second string and not match compare the character like we do in,
10 == 10
I hope now you have a clarity now.
i will read this explanation of yours and revert back. And i know that website and i even use it but the thing i said was that i can just check my answers there. That website doesnt help me think code. I approach the the code and my code is different and wrong than the code in answer. That breaks me down always as having a tough time solving these practice problems. Ofcourse i can check the answer using the visualiser but that wont help me. If you have any insights on this then i am open to talk .Thank you i have to take some time to process your explanation. Be in touch.
I have tweaked your code a little bit and used
ord function which returns an integer representing the Unicode character.
s1 = 'I have been busier these days due to having a lot on my plate.'
s2 = 'You have been very supportive towards my recent endeavors.'
common = 
for i in s1:
for j in s2:
if ord(i) == ord(j) and i not in common:
This will lead to your output by using
== operator. Hope now we are on same page.
i will check this too. I am currently understanding your first reply as i was caught up in some work. Thanks for your support i will reply you once i see both of your explanations bro
yes i understood the ord function which you tweaked in my code. It was easier to understand. now when i see my code i went wrong because in the list here were strings and it cant be processed because it is not integer? is that what your first explanation says?
Yes, correct. We can’t take it as an index.
No worries. Take your time.
okay then i got it. Thank you for your help. I got both.
I didn’t know about this visualization tool!!! Wow!!! This is going to be so helpful!