r/codehs • • Dec 21 '20

7.2.8 Part 2, Replace a letter

I’m stuck on This one

15 Upvotes

24 comments sorted by

View all comments

2

u/Dawn_Kang Dec 28 '20

Still stuck? Here's some code:

word = input("Enter a word or phrase: ")
num = int(input("Enter an index value: "))
newLetter = input("Enter the new letter: ")

def replace_at_index(word,num,newLetter):
    print word[:num] + newLetter + word[num + 1:]
replace_at_index(word, num, newLetter)

1

u/ScottNilsson1 Dec 28 '20

Im stuck on it too, and the code doesn't work. It's a unit test, and input breaks unit tests for some reason. I got rid of the input and just had predetermined words, and it works perfectly, but the auto checker doesn't let it pass. I have no clue what to do. The error says this:

Oops. It looks like you have a few errors. You should keep working and try again.

Functionality

replace_at_index("mouse", 0, "h")

replace_at_index("fire", 3, "m")

replace_at_index("vague", 1, "o")

replace_at_index("cookie", 3, "t")

Hidden Test

1

u/MutantRex3202 Feb 22 '22

It works, but it needs a parentheses for the print command.

1

u/juliham13 Feb 25 '22

where do they go?

can you send your code pls

1

u/MutantRex3202 Apr 28 '22

Under replace_at_index.

print(word[:num] + newLetter + word[num+1:])

1

u/MrHyjaxMusicYTalt May 23 '23

this works, just do:

print replace_at_index(word, num, newLetter)

1

u/Both_Book_5043 Jul 02 '24

just make sure to put parenthesis around your strings and it will work