calthoff / tstp

This is an old repository for the exercises in "The Self-Taught Programmer." Please see /selftaught.
167 stars 190 forks source link

chap6_challenge4.py wrong code #39

Open T-Zema opened 4 years ago

T-Zema commented 4 years ago

The answer is bad

lst = "Where now? Who now? When now?".split("?") print(lst) ['Where now', ' Who now', ' When now', '']

the output should be like this like in the task description

['Where now?', ' Who now?', ' When now?']

hardikg2907 commented 4 years ago

Yeah, the issue could be resolved by using a different approach but i guess it will include a for loop which is actually in the next chapter.

PyProgrammer289 commented 3 years ago

The answer is bad

lst = "Where now? Who now? When now?".split("?") print(lst) ['Where now', ' Who now', ' When now', '']

the output should be like this like in the task description

['Where now?', ' Who now?', ' When now?']

Try this:

a= "Where now? Who now? When now?"

b=a.split("?")

b[0]="Where now?" b[1]="Who now?" b[2]="When now?"

c=b[:3]

print(c)

Let me know if you need any help with explanations

yosso commented 2 years ago

I think you should check for the question mark and a space => "? ", not just a single space...then check to see if there is a need to append the "?".

def ChkForStr(s,c):
    if s[-1] == c:
        return ""
    else:
        return c

delim = "? "
a = "Where now? Who now? When now?".split(delim)
a[0] = a[0] + ChkForStr(a[0], "?")
a[1] = a[1] + ChkForStr(a[1], "?")
a[2] = a[2] + ChkForStr(a[2], "?")
print (a)
ghost commented 2 years ago

### Use the splice method instead text="where now? who now? when now?"

a=text[0:10] b=text[11:19] c=text[-9:]

new_text=[a,b,c]

print(new_text)

ghost commented 2 years ago

The answer is bad

lst = "Where now? Who now? When now?".split("?") print(lst) ['Where now', ' Who now', ' When now', '']

the output should be like this like in the task description ['Where now?', ' Who now?', ' When now?']

Try this:

a= "Where now? Who now? When now?"

b=a.split("?")

b[0]="Where now?" b[1]="Who now?" b[2]="When now?"

c=b[:3]

print(c)

Let me know if you need any help with explanations

i dont know if this is the right solution. objects in print(b) have no questionmark so you reassign the values with the correct strings. defeats the purpose of the code in a way.

GitGrunfelder commented 2 years ago

str = "Where now? Who now? When now?" str_list = [] str_list = str.split("? ") str_list[0] = str_list[0] + "?" str_list[1] = str_list[1] + "?" print(str_list)

The question asks you to return a list, so first assign the sentence to a variable. Then initiate a list. Then split the string at the ? . (You could leave out the space behind the ? but then you need to concatenate the final index as well) Now you have a list with each question missing a question mark. Concatenate a question mark to the two indexes in the list that are missing it.

m-townsend commented 11 months ago

I came up with this by chaining string methods together. Adding the additional character "," after the "?" with the replace method. This created a distinction between the spaces between words, and the spaces between the questions. Then split on the comma to get a list.

string = 'Where now? Who now? When now?'
string_list = string.replace('? ','?,').split(',')
print(string_list)
NobbyNolan commented 8 months ago

I came up with this by chaining string methods together. Adding the additional character "," after the "?" with the replace method. This created a distinction between the spaces between words, and the spaces between the questions. Then split on the comma to get a list.

string = 'Where now? Who now? When now?'
string_list = string.replace('? ','?,').split(',')
print(string_list)

Ah yes, I did this replacing "?" with "?!" and was then confused that it created a list of length 4, the fourth being " ". I understand that this was because I didn't replace the " " as you did and therefore split on the final "?" too.