I can just use return?

How am i supposed to know from the introduction on this lesson that i can just type : return letter in word?? Also i have no idea how :

Write a function called common_letters that takes two arguments, string_one and string_two and then returns a list with all of the letters they have in common.

Is equal to:
def common_letters(string_one, string_two):
common =
for letter in string_one:
if (letter in string_two) and not (letter in common):
common.append(letter)
return common

I am so so so lost. I dont understand loops at all and now strings are killing me. I feel like I am too stupid to understand the lessons.

4 Likes

Did you not read the white column with the exercise-specific guide on the left? Not to mention, what you wrote works perfectly well, so I think you’re doing fine.

(Don’t mind me, I’m just here to complain about the compiler throwing TabErrors and IndentationErrors at me left and right.)

2 Likes

I’m glad I’m not alone in all this. I need to check out these community pages more often, since apparently I’m not alone in my constant misery going through these exercises.

They are EXTREMELY poorly written and expect us to have more knowledge about Python than we get from this course so far. It’s just physically impossible to do this one with just what we learned. What is even “and not”?! I mean, I know what it does but so far it was never used so I had no idea this exists in Python, godd*mn it , this is frustrating.

You’ve probably been introduced to and as well as not, so using both in the same expression isn’t new

and determines whether both are true
not inverts a boolean

I’m not the smartest tool in the shed so thinking like a programmer comes hard for me at this moment. The way this course works I feel like it thaught me pieces of the puzzle but not how to connect them. I know about AND and NOT but it wouldnt come to my mind at this stage that I can use them together.

A solution to some problem is going to have a bunch of components involved in it.

You might pick a few that you know need to be there, and when some puzzle pieces are in place you get more information about what needs to go in-between them.

There are two conditions involved, and they both need to pass, so you’d use AND to combine their results.

condition1 and condition2

One condition is that the letter is present in the other word.

present in the other word and condition2

The other condition is that the letter isn’t already included

(present in the other word) and (not already included)

so they’re… separate things, right? yeah they’re in the same expression but they don’t combine into some kind of new thing.

And there isn’t a single correct solution that you need to find either. You’d look at the problem and go hmm what could I do to make this happen? And then break that up into small parts and start implementing them one at a time. You would for example need to test whether the character is present, but you need it to be inverted, so you’d look around for something that can alter the result you currently have into what you need, that’s not, so you’d add not to your condition.

1 Like

this is my solution:

def common_letters(string_one, string_two):
  letters = []
  for letter in string_one.lower():
    if letter in string_two.lower() and letter not in letters:
      letters.append(letter)
  return letters

print(common_letters("banana", "cream"))

Hello, @mohamedeldimardash51.

I have one idea for you to consider. How many times does your code call .lower()? How many times is it necessary to call .lower()? Once for each string would suffice.

2 Likes

Hi @midlindner
as we have 2 different strings, so they have both to be of the same case(lowercase) string_one and string_two

True, but keep in mind that code inside a loop gets repeated with each iteration. Converting the string outside the loop, and saving it would only call the .lower() function once for the string instead of repeatedly.

1 Like
def common_letters(string_one, string_two): # defines a new function called 'common_letters'
    common = [] # This is a new empty list

    for letter in string_one: # Goes through each letter in string_one and executes the following code
        if (letter in string_two) and not (letter in common): # if letter is in string two and not in common list
            common.append(letter) # adds the letter to common list as a string

    return common # You can use return when you are in a function and it ends the function
1 Like