def get_score(word):
    '''(str) -> int

    Return the point value the word earns.

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

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

    I have this homework I want some help ,to guide me from where should I begin 
    this my attempt
    s=0
    if len(word) in range (6,10)------for  the second condition
    for i in range (len(word)):
    s=s+i
    but it didnot work correctly any help  please

Dani AI

Generated

Two quick things to fix first, then a clean approach to implement and test.

The docstring's length bands are ambiguous and overlap; as and noted, decide on non‑overlapping, explicit boundaries (for example, use inclusive comparisons). Ambiguous rules lead to wrong branching. Also remember the immediate bug pointed out: your function must return the computed value.

Avoid the loop pattern you tried that accumulates the loop index. Adding the loop index each iteration produces 0 + 1 + 2 + ... which is not "one point per character". If the rule gives a fixed number of points per character, compute the score by picking the per‑character multiplier from a clear if/elif/else chain and multiply that by the word length. That is simpler, faster, and less error prone than iterating character-by-character.

Watch Python range and boundary tests: range(a,b) covers a..b-1, so using it without care is a common off‑by‑one trap. Express boundaries with comparisons such as 3 <= length <= 6 to make intent explicit. Follow and implement the selection of the multiplier with if/elif/else so only one branch runs.

Testing checklist: verify the function returns the correct integer; test the small cases (<3), each boundary value (3, 6, 7, 10, 11), and a few intermediate lengths; compare results to manual calculations. Add a couple of assert checks or prints while developing, then remove them. With a clear spec, a single length lookup and a multiply-and-return step will make the function correct and easy to read.

Recommended Answers

All 4 Replies

In the future, please let give more information about how the program is failing, otherwise we won't have enough to go by.

As it happens, the answer is quite simple: the function doesn't return a value. Try adding return s at the end of the function and it should work correctly.

def <font style='color:black; background-color:Lime;'>word_score</font>(word):
    '''(<font style='color:black; background-color:Lime;'>str</font>) -> int

    Return the point value the word earns.

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

    >>> <font style='color:black; background-color:Lime;'>word_score</font>('DRUDGERY')
    16
    '''
    <font style='color:black; background-color:Lime;'>s=</font>0
    if <font style='color:black; background-color:Lime;'>len</font>(word) in range (6,10):
        for i in range (<font style='color:black; background-color:Lime;'>len</font>(word)):
            s += i
    return s

if <font style='color:black; background-color:Lime;'>__name__</font> == "<font style='color:black; background-color:Lime;'>__main__</font>":
    print(<font style='color:black; background-color:Lime;'>word_score</font>('DRUDGERY'))

I ran this and got a score of 28 for the word DRUDGERY. I know it isn't the correct score, but it should at least get you started.

BTW, the score ranges as given don't make much sense; I suspect that they should be

    Word length: < 3: 0 points
                 3-6: 1 point per character in word
                 7-10: 2 points per character in word
                 11+: 3 points per character in word

Otherwise, you have overlapping score ranges, which would be problematic - not to mention a gap between 3 and 6.

define get_score(with a string argument):
    length = len(string)
    score = 0
    if length < 3:
        score = 0
    elif score > 6:
        score = length multiplied by 1
    elif other condition:
        apply other rules to score
    ...
    return score

Erratum :)

line 6: elif length > 6:

Like Schol-R-LEA (Joseph Osako) pointed out the rukes have to be

'''
Let's assume the rules are as follows
Word length: < 3: 0 points
             3-6: 1 point per character in word
             7-10: 2 points per character in word
             11+: 3 points per character in word
'''

for you to get the correct score.
The code boils down to a series of
if
elif
else
statements

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.