Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ThinkPython Exercise 9.2
#1
Help please!

I am starting to learn python using the ThinkPython book. For exercise 9.2, I wrote the below code (which is the same as the answer code posted online). However, the conditional statement only recognizes the first letter. For example, if the word starts with the letter 'e' or 'E' (such as "elephant"), the statement returns False and for all others, including words containing the letter 'e' or 'E' but not at the beginning (such as "love"), the statement will return True. Can someone please point it out to me what I am doing wrong? Thank you very much!!

fin=open('words.txt')
def has_no_e(word):
    char=""
    for char in word:
        if char == 'E' or char == 'e':
            return False
        return True
Reply
#2
I can't tell much since there is no indentation. Add a print(char) after "for char in word" and see if that helps you understand.
Reply
#3
I probably shouldn't be answering, but I think I see the problem here. You should as an "else" to the conditional.

...
if char == 'E' or char == 'e':
    return False
else:
    return True
Oh, and I think it's good form to use code tags--it's the Python symbol towards the right of the format bar.
Reply
#4
the problem with your original code is the indentation level of the last line - it should be out of the for loop. This should work
fin=open('words.txt')
def has_no_e(word):
    char=""
    for char in word:
        if char == 'E' or char == 'e':
            return False
    return True
but it's better like this
def has_no_e(word):
    return not 'e' in word.lower()
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
(May-23-2018, 12:49 AM)malonn Wrote: I probably shouldn't be answering, but I think I see the problem here. You should as an "else" to the conditional.

...
if char == 'E' or char == 'e':
    return False
else:
    return True
Oh, and I think it's good form to use code tags--it's the Python symbol towards the right of the format bar.

Just saying - since that was anyway wrong answer - that expression already produces boolean
not (char == 'E' or char == 'e')
but that one will be shorter
char not in 'eE'
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#6
Okay, good to know. My fault, @sangurocactus--like I said, I probably shouldn't answer.
Reply
#7
Hi Buran - thank you very much. The problem is indeed in the indention.

Thanks everyone else for their input!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  ThinkPython Exercise 9.3 sangurocactus 13 8,356 Jun-04-2018, 05:37 PM
Last Post: sangurocactus

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020