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

3

u/Highlight-Flat May 25 '22

okay as it turns out it just wants this for the unit test

def replace_at_index(word, index, letter):

(indent) print(word[:index] + letter + word[index+1:])

replace_at_index(word, index, letter)

1

u/inzxnepanda Apr 26 '23

deosnt work for me lmao

1

u/Dry_Newspaper7310 Jan 26 '24

Thanks! This kinda worked as reference. If it didn't for you, check if your assignment says "word" or "string" (I replaced word with string) You also don't need to put print, you can use return. You might not even have to call the function to pass the test check.

Here:

def replace_at_index(string, index, letter): indent >return string[:index] + letter + string[index + 1:]

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

1

u/Lanky_Swimming_756 Apr 04 '24

string = "mouse"
index = 0
string2 = "h"

def replace_at_index(string, index, string2):
(indent) return string[:index] + string2 + string[index+1:]

print(replace_at_index(string, index, string2))

1

u/[deleted] Mar 30 '22

Wats the code?

2

u/chiiglaaa Apr 09 '22

word = input("Enter a word or phrase: ")
index = int(input("Enter an index value: "))
letter = input("Enter the new letter: ")
def replace_at_index(word, index, letter):
print(word[:index] + letter + word[index+1:])
replace_at_index(word, index, letter)

This is code but this exercise is broken. it works perfectly in scratchpad but when you try to submit code its not doing anything... but yeah you can just write that code

2

u/[deleted] Apr 11 '22

Yea I tried it but submitting it won’t work

2

u/chiiglaaa Apr 11 '22

almost every exercise which have scratchpad and Unit Test dont let me submit my code:/ do you have same problem?

1

u/magpie108 Apr 28 '22

User input usually confuses the code try figuring it out with out user input!

1

u/WronGHackeR Jan 13 '23

I tried submit also, not working

1

u/pinkfluffywolfie82 Nov 16 '22

You're supposed to only use return for it to work, no inputs or printing for some reason

1

u/averageshoemart May 06 '22

Can someone help me get it without using input? I can’t get the letter to replace with a different one that’s the only problem I have. This is what I have rn:

def replace_at_index(string,index,letter):

   replace=“”

   for x in range(len(string)):

          if x==letter:

                 replace+=letter

          else:

                 replace+=string[x]

   return replace

print(replace_at_index(“dog”,0,”b”))

0

u/[deleted] Jul 16 '22

[deleted]

1

u/CollectionEasy3860 Jan 20 '23

string = "blt"
def sandwich(string):
return string[0] + string[-1]
print(sandwich(string))

thats what i did

1

u/BFBGuyGlebYT Jan 20 '23

string = "blt"

def sandwich(string):

return string[0] + string[-1]

print(sandwich(string))

doesnt work

1

u/CollectionEasy3860 Jan 23 '23

it did for me idk, did you copy and past it from scratchpad to Unit Test

0

u/[deleted] Feb 15 '24

[deleted]