### Thread: Question About Elif Error In Word Game Program

1. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Aug 2013
Posts
232
Rep Power
1

#### Question About Elif Error In Word Game Program

I am making a score calculation program that requires a user to enter a word, and he will get a corresponding score based on the amount of characters that are in the word itself.

My problem is that Python is highlighting my second elif statement in red for some reason, is it the pair of parenthesis that are causing the error to occur?

Here's my code:

Code:
```def word_score(word):
""" (str) -> int

Return the point value the word earns.

Word length: < 3: 0 points
3-6: 1 point per character for all characters in word
7-9: 2 points per character for all characters in word
10+: 3 points per character for all characters in word

>>> word_score('DRUDGERY')
16
"""

number = 0

if (len(word) < 3):

return number

elif (len(word) >= 3 && len(word) <= 6):

number = len(word)

return number

elif (len(word) >= 7 && len(word) <= 9):

number = 2*len(word)

return number

else:

number = 3*len(word)

return```
Last edited by HauntJemimah; September 27th, 2013 at 09:41 AM.
2. Where you have "&&" should be "and".

That would be easier to spot if I weren't a C programmer.

Here are three versions of the scoring function. In the first, we simply use the numerical value of logical expressions. This comes from my APL background, www.jsoftware.com . The second program is almost the same as yours except that I discarded all the tests that had already been done, and made one other change. Some people think there should be only one return from a function. Multiple returns for a short function like that don't trouble me. The third function is yours with the && problem fixed.
Code:
```'''
\$ python q.py
0 0 0
0 0 0
0 0 0
3 3 3
4 4 4
5 5 5
6 6 6
14 14 14
16 16 16
18 18 18
30 30 None
33 33 None
36 36 None
39 39 None
42 42 None
'''

def ws1(word):
n = len(word)
return n * ((2<n) + (6<n) + (9<n))

def ws2(word):
if len(word) < 3:
return 0
if len(word) <= 6:
return len(word)
if len(word) <= 9:
return 2*len(word)
return 3*len(word)

def word_score(word):
""" (str) -> int

Return the point value the word earns.

Word length: < 3: 0 points
3-6: 1 point per character for all characters in word
7-9: 2 points per character for all characters in word
10+: 3 points per character for all characters in word

>>> word_score('DRUDGERY')
16
"""
number = 0
if (len(word) < 3):
return number
elif (len(word) >= 3 and len(word) <= 6):
number = len(word)
return number
elif (len(word) >= 7 and len(word) <= 9):
number = 2*len(word)
return number
else:
number = 3*len(word)
return

# a test.
for i in range(15):
word = 'a'*i
print(ws1(word), ws2(word), word_score(word))```
3. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Aug 2013
Posts
232
Rep Power
1
Originally Posted by b49P23TIvg
Where you have "&&" should be "and".

That would be easier to spot if I weren't a C programmer.

Here are three versions of the scoring function. In the first, we simply use the numerical value of logical expressions. This comes from my APL background, www.jsoftware.com . The second program is almost the same as yours except that I discarded all the tests that had already been done, and made one other change. Some people think there should be only one return from a function. Multiple returns for a short function like that don't trouble me. The third function is yours with the && problem fixed.
Code:
```'''
\$ python q.py
0 0 0
0 0 0
0 0 0
3 3 3
4 4 4
5 5 5
6 6 6
14 14 14
16 16 16
18 18 18
30 30 None
33 33 None
36 36 None
39 39 None
42 42 None
'''

def ws1(word):
n = len(word)
return n * ((2<n) + (6<n) + (9<n))

def ws2(word):
if len(word) < 3:
return 0
if len(word) <= 6:
return len(word)
if len(word) <= 9:
return 2*len(word)
return 3*len(word)

def word_score(word):
""" (str) -> int

Return the point value the word earns.

Word length: < 3: 0 points
3-6: 1 point per character for all characters in word
7-9: 2 points per character for all characters in word
10+: 3 points per character for all characters in word

>>> word_score('DRUDGERY')
16
"""
number = 0
if (len(word) < 3):
return number
elif (len(word) >= 3 and len(word) <= 6):
number = len(word)
return number
elif (len(word) >= 7 and len(word) <= 9):
number = 2*len(word)
return number
else:
number = 3*len(word)
return

# a test.
for i in range(15):
word = 'a'*i
print(ws1(word), ws2(word), word_score(word))```
Thanks a bunch! I can't believe I didn't use and like they mentioned in that course, oops heh.

I happen to like C++ a lot in comparison to the languages I'm learning (C++, Java, C# (with XNA game programming) and Python) so I understand. Its just that the syntax is a bit easier to understand, but a lot of the languages start to interblend as I'm finding out going deeper into each language