-1

I need to create a function that returns a string value and then I print the value by passing a list to the function and then calling the function. The output would look like below :

The 7 sight words for this week are new, barn, shark, hold, art, only and eyes.
The 2 sight words for this week are subtract and add
The 9 sight words for this week are girl, house, best, thing, easy, wrong, right, again, and above.
The only site word for this week is question
There are no new sight words for this week!

My current code give's me the variables (except for str1) but only passes 1 value of the list. I can accomplish this using Join but the instructor specifically said not to use Join. However, even with Join I was struggling with how to get the 'and' value in the proper location. Any guidance would be appreciated!

Current Code :

def createSentence(list):
    list1 = list
    str1 = 'There are no new sight words for this week!'
    str2 = 'The only site word for this week is '
    str3 = 'The ' + str(len(list)) + ' sight words for this week are '

    for list1 in list:
        if len(list) == 0:
            return str1
        elif len(list) == 1:
            return(str2 + list1)
        elif len(list) > 1:
            return(str3 + list1 + ',')
        
    
week1 = ['new', 'barn', 'shark', 'hold', 'art', 'only', 'eyes']
week2 = ['subtract', 'add']
week3 = ['girl', 'house', 'best', 'thing', 'easy', 'wrong', 'right', 'again', 'above']
week4 = ['question']
week5 = []

print(createSentence(week1)) 
print(createSentence(week2))
print(createSentence(week3))
print(createSentence(week4))
print(createSentence(week5))
  • 1
    Is your question how to join without using ``str.join``, or how to join using ``and`` as the last separator? – MisterMiyagi Feb 04 '22 at 21:11
  • As @MisterMiyagi has surmised, you might find your answer here: https://stackoverflow.com/questions/19838976/grammatical-list-join-in-python – JonSG Feb 04 '22 at 21:59

3 Answers3

0

Solution not using .join().

Firstly, never overwrite built-in functions unless you have a really good reason. In your question, don't use list as a variable name.

My solution is below, with PEP-8 compliance and your list variable renamed. This solution is using f-strings to replace some of the functionality of .join() and uses enumerate() to get an index of the list for the edge cases, the first index for no comma and the last index for the "and"

def create_sentence(alist):
    if len(alist) == 0:
        return 'There are no new sight words for this week!'
    elif len(alist) == 1:
        return f'The only site word for this week is {alist[0]}'
    elif len(alist) > 1:
        for index, word in enumerate(alist):
            if index == 0:  # no comma, first index
                new_sentence = f"{word}"
            elif index == len(alist)-1:  # no comma, add and, last index
                new_sentence = f"{new_sentence} and {word}"
            else:
                new_sentence = f"{new_sentence}, {word}"
        return f'The {len(alist)} sight words for this week are {new_sentence}'

output

The 7 sight words for this week are new, barn, shark, hold, art, only and eyes
The 2 sight words for this week are subtract and add
The 9 sight words for this week are girl, house, best, thing, easy, wrong, right, again and above
The only site word for this week is question
There are no new sight words for this week!
tgpz
  • 161
  • 10
-1

Rather than putting your if statement in the loop, put the loop in the if statement. Something like

    if len(list) == 0:
        return str1
    elif len(list) == 1:
        return(str2 + list1)
    elif len(list) == 2:
        return(str3 + f'{list[0]} and {list[1]}':
    else:
        x = str3
        for word in list[:-1]:
            x += f'{word}, '
        x += f' and {list[-1]}.'

The prohibition against using join seems a little silly, as it doesn't even replace the entire body of the else clause. It's not like using join does everything for you.

chepner
  • 497,756
  • 71
  • 530
  • 681
  • Yeah I attempted this at some point, but for some reason it was saying I couldn't concatenate list with the str variables - Even if I cast it as str. Thanks for the direction! – kirito04 Feb 04 '22 at 21:05
-1

You're returning on the first iteration of the loop, not processing all the words in the list. Also, if the list is empty, the loop will never even run.

You should have the if satement outside the loop. You only need a loop when there's more than 1 word. In this loop, you should concatenate each word to the return string. You'll need to treat the last two words specially to put "and" between them instead of ",".

def createSentence(list1):
    str1 = 'There are no new sight words for this week!'
    str2 = 'The only site word for this week is '
    str3 = 'The ' + str(len(list)) + ' sight words for this week are '

    if len(list1) == 0:
        return str1
    elif len(list1) == 1:
        return(str2 + list1[0])
    elif len(list1) > 1:
        for word in list1[:-2]:
            str3 += word + ', '
        str3 += list1[-2] + " and " + list1[-1]
        return str3
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Ah, I did not even think to slice like that for the "and" and commas. This makes sense and also explains the errors I was getting with concatenating the full string. Since you pull a value out of the string, it allows me to concatenate those value/values without error. Thank you! – kirito04 Feb 04 '22 at 21:09