'Create a function that gets passed a list and returns a string value NOT using Join
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))
Solution 1:[1]
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!
Solution 2:[2]
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
Solution 3:[3]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | tgpz |
| Solution 2 | Barmar |
| Solution 3 |
