Within a for loop, does every if statement need an else?

Question

In the context of this exercise, within a Python for loop, does, or should, every if statement have an else statement?

Answer

No, for if statements in general, whether or not they are part of a for loop, they do not always need to be followed by an else statement. (This may be helpful to keep in mind for this exercise!)

If nothing else should happen if the condition is False, then it might make sense to exclude the else entirely. This not only saves space, by reducing the code by a few lines, but can also potentially speed up a program, by moving to the next iteration of a loop immediately instead of running additional code per iteration.

8 Likes

Isn’t it nearly always better to include the ‘else’ statement for readability?
Surely, the performance gain of excluding it is negligible.

1 Like

18 posts were split to a new topic: Why doesn’t else: return False work?

Not necesarily. Sometime you just want to check one condition. Take as example that you want to prepare a coffe with milk. Supposing you already have the coffe, you will go and check in your fridge if you have milk. If yes you add it. Anyother result you do nothing.

9 Likes

" 'str’ object is not callable "

What does it means?
I did not get any error from the terminal of output, but I got this message.

Here is the query : -
1.

Write a function called letter_check that takes two inputs, word and letter .

This function should return True if the word contains the letter and False if it does not.

MY CODE -

def letter_check(word, letter):

for word in letter:

counter = 0

digit = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

if word(digit):

  False 

else:

  True

return letter_check

Only ‘functions’ are callable objects.

Somewhere in your code a string must be getting treated as a function. Study the traceback for a line number where the error was raised.

We can see it now…

1 Like

@mtf Thanks a lot.
But what was the solution?
Could you please express further more? :slight_smile:

1 Like

Have you given the problem another attempt? The word ‘contains’ indicates membership. Is there a common Python keyword that might indicate the same thing? in comes to mind. We can use it in an if statement, or in a logical expression.

if x in y:

or

return x in  y

In that capacity we could call in a membership operator.

@mtf No I am passed out in the third attempt :sweat_smile:

Now I understood.
if list1 in list2 or returned list1 in list2 then I have to call “in” which is a membership operator.
:slight_smile: Thanks one more time!

1 Like

Be sure to dig into this before going forward. This is a learning moment. Absorb it.


Also, it would be a drag not to see your working solution. Please keep us posted.

@mtf So, could I message you personally if queries are arrived? Yesterday I saw there’s a button of ‘Message’.
What do you say

Currently I’m on the unit of Python : Modules in Computer Science career path

The only way I can offer individual support is in a topic, not a DM. This permits public involvement, and perhaps correction of anything I may have stated. I’m open to that, and willing to bear that scrutiny in the aim of correctness. To my mind, this is better for you.

3 Likes

Fine to know.
Just when I was doing my stuff, I lost my panel.
@mtf could you please help me for that?

Screenshot:-

**What can I do? I am in tense. **
I did refresh but it didn’t works.

hey mtf, I am confused by this code in this exercise, I don;t seem to get it the line “for character in word” in the correct answer, see below. I thought when we iterate a list, we iterate the entire list, but word is an argument/input, how can we assign a variable to an argument?? shouldnt the code be like this:

correct answer:
def letter_check(word, letter):
for character in word:
if character == letter:
return True
return False

my answer:
def letter_check(word, letter):
for word in letter_check:
if word == letter:
print (True)
else:
print(False)

thanks for your help!

The object after in needs to be an iterable. letter_check is the name of your function. The iterable is actually, word.

We’re looking for a letter in the word…

for char in word:

We are given to assume that word is a character string, which we may iterate, one character at a time.

The function should return a boolean, as opposed to printing it.

Later on, once you have more under your belt, including basic logic, you’ll be able to simplify the function to a single return statement.

return letter in word

The logic of the above rests on the in operator. The expression is a membership test which will yield a boolean.

2 Likes