Python Forum
Error with simple "or" statement?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error with simple "or" statement?
#1
I'm trying to count the number of words in a sentence that include "o" or "e." Why does this count every word?

player_str = "the best player in the world was nomar garciappara"
#Enter your code below
num_o_or_e = 0
s = player_str.split()
print(player_str)
print(s)

for word in s:
    if 'o' or 'e' in word:
        print(word)
        num_o_or_e = num_o_or_e + 1
print('The number of words with an o or e in specified sentence is',num_o_or_e)
Reply
#2
https://python-forum.io/Thread-Multiple-...or-keyword
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
#3
(Nov-15-2019, 03:16 PM)buran Wrote: https://python-forum.io/Thread-Multiple-...or-keyword

That makes sense.

I changed it to this:

player_str = "the best player in the world was nomar garciappara"
#Enter your code below
num_o_or_e = 0
s = player_str.split()
print(player_str)
print(s)
 
for word in s:
    if 'o' in word:
        print(word)
        num_o_or_e = num_o_or_e + 1
    elif 'e' in word:
        print(word)
        num_o_or_e = num_o_or_e + 1
    else: continue
print('The number of words with an o or e in specified sentence is',num_o_or_e)
This works, but is there an easier way that would involve restructuring only the "if 'o' or 'e'" line?
Reply
#4
well, the example from the link I posted:
if 'o' in word or 'e' in word:
or
if any(char in word for char in 'oe'):
or more exotic
if set('oe') & set(word):
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
(Nov-15-2019, 04:45 PM)buran Wrote: well, the example from the link I posted:
if 'o' in word or 'e' in word:

Oops... I added a second if.

Thanks!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  An unexplainable error in .format statement - but only in a larger piece of code? ToniE 4 696 Sep-05-2023, 12:50 PM
Last Post: ToniE
  Error in Using INPUT statement gunwaba 1 2,068 Jul-03-2022, 10:22 PM
Last Post: deanhystad
  Cryptic Error with import statement Led_Zeppelin 2 2,536 Jan-11-2022, 01:13 PM
Last Post: Led_Zeppelin
  Indentation Error With If Else Statement JoeDainton123 3 1,810 Aug-02-2020, 08:29 PM
Last Post: deanhystad
  Simple code error please help bntayfur 7 3,449 Jun-08-2020, 02:22 AM
Last Post: pyzyx3qwerty
  syntax error on return statement l_butler 5 3,083 May-31-2020, 02:26 PM
Last Post: pyzyx3qwerty
  Adding second message to simple loop error PythonGainz 2 2,075 Apr-06-2020, 11:55 AM
Last Post: PythonGainz
  Beginner - simple package installation error mefeng2008 0 1,717 Mar-13-2020, 09:17 AM
Last Post: mefeng2008
  Simple python code error cls0724 5 3,320 Mar-12-2020, 07:45 PM
Last Post: stullis
  Error in continue statement Man_from_India 1 1,542 Feb-02-2020, 02:23 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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