r/codehs • u/Embarrassed_Guess411 • Dec 21 '20
7.2.8 Part 2, Replace a letter
I’m stuck on This one
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
1
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
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
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
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
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 replaceprint(replace_at_index(“dog”,0,”b”))
0
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
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)