Question

In: Computer Science

Can you tell me what is wrong with this code ? def update_char_view(phrase: str, current_view: str,...

Can you tell me what is wrong with this code ?

def update_char_view(phrase: str, current_view: str, index: int, guess: str)->str:
if guess in phrase:
current_view = current_view.replace(current_view[index], guess)
else:
current_view = current_view
  
return current_view

update_char_view("animal", "a^imal" , 1, "n")
'animal'
update_char_view("animal", "a^m^l", 3, "a")
'aamal'

Solutions

Expert Solution

Answer:

What's wrong in your code?

current_view = current_view.replace(current_view[index], guess)

at that line when you are trying to replace the value at that index the replace method will take two argument first argument which denotes the string to be replaced with second argument. So when you are giving the index value at that particular index value thre is '^' so the replace method checks '^' for that character all over the string and its replacing with the guess character which we don't need that. according to your code your only requirment is to replace the guessed string at the given index so we need to update the above statement using string slicing and concatenation

here is the updated line : current_view=current_view[:index]+guess+current_view[index+1:]

and that else condtion in your code doesn't make any sense you can remove that

Python code:

Raw code:

def update_char_view(phrase: str, current_view: str, index: int, guess: str)->str:

if guess in phrase:

current_view=current_view[:index]+guess+current_view[index+1:]

return current_view

print(update_char_view("animal", "a^imal" , 1, "n"))

print(update_char_view("animal", "a^m^l", 3, "a"))

Editor:

output;

Hope this helps you! If you still have any doubts or queries please feel free to comment in the comment section.

"Please refer to the screenshot of the code to understand the indentation of the code".

Thank you! Do upvote.


Related Solutions

Can someone tell me what is wrong with this code? Python pong game using graphics.py When...
Can someone tell me what is wrong with this code? Python pong game using graphics.py When the ball moves the paddles don't move and when the paddles move the ball doesn't move. from graphics import * import time, random def racket1_up(racket1):    racket1.move(0,20)        def racket1_down(racket1):    racket1.move(0,-20)   def racket2_up(racket2):    racket2.move(0,20)   def racket2_down(racket2):    racket2.move(0,-20)   def bounceInBox(shape, dx, dy, xLow, xHigh, yLow, yHigh):     delay = .005     for i in range(600):         shape.move(dx, dy)         center = shape.getCenter()         x = center.getX()         y = center.getY()         if x...
Okay, can someone please tell me what I am doing wrong?? I will show the code...
Okay, can someone please tell me what I am doing wrong?? I will show the code I submitted for the assignment. However, according to my instructor I did it incorrectly but I am not understanding why. I will show the instructor's comment after providing my original code for the assignment. Thank you in advance. * * * * * HourlyTest Class * * * * * import java.util.Scanner; public class HourlyTest {    public static void main(String[] args)     {        ...
Can you please tell me, what would be the correct way of doing it? def updateRepresentation(blank,...
Can you please tell me, what would be the correct way of doing it? def updateRepresentation(blank, secret, letter): """ This function replaces the appropriate underscores with the guessed letter. Eg. letter = 't', secret = "tiger", blank = "_i_er" --> returns "ti_er" Paramters: blank, secret are strings letter is a string, but a single letter. Returns: a string """ #TODO -- complete this function so that it produces a new string #from blank with letter inserted into the appropriate locations....
can someone tell me if i am wrong on any of these???? THANKS In order to...
can someone tell me if i am wrong on any of these???? THANKS In order to be able to deliver an effective persuasive speech, you need to be able to detect fallacies in your own as well as others’ speeches. The following statements of reasoning are all examples of the following fallacies: Hasty generalization, mistaken cause, invalid analogy, red herring, Ad hominem, false dilemma, bandwagon or slippery slope. 1. __________bandwagon fallacy_______ I don’t see any reason to wear a helmet...
Can someone look into my code and tell me what do you think: Thats Palindrome; //class...
Can someone look into my code and tell me what do you think: Thats Palindrome; //class name Palindrome public class Palindrome {    public static void palindromeChecker(String... str) {        // takes string one by one        for (String s : str) {            // creates a stringbuilder for s            StringBuilder sb = new StringBuilder(s);            // reverses the sb            sb.reverse();            // checks if both...
I need to fix this code, and could you please tell me what was the problem...
I need to fix this code, and could you please tell me what was the problem options 1 and 9 don't work #include <stdio.h> #include <time.h> #include <stdlib.h> // generate a random integer between lower and upper values int GenerateRandomInt(int lower, int upper){     int num =(rand()% (upper - lower+1))+lower;     return num; } // use random numbers to set the values of the matrix void InitializeMatrix(int row, int column, int dimension, int mat[][dimension]){     for(int i =0; i<row; i++){...
Can you pleaes tell me what is the job responsibilites of an Accounts Payable Clerk is?...
Can you pleaes tell me what is the job responsibilites of an Accounts Payable Clerk is? also What is Expense Coding?
What can you tell me about being a CFE and all the Role entails?
What can you tell me about being a CFE and all the Role entails?
Can you tell me what type of study is this (exploratory, descriptive or explanatory) and why?...
Can you tell me what type of study is this (exploratory, descriptive or explanatory) and why? justify the answer! The chief financial officer of a multinational corporation wants to know the profits made during past 5 years by each of the subsidiaries in England ,Germany, France and Spain.
What can you tell me about analyzing the statistical significance and p-values
What can you tell me about analyzing the statistical significance and p-values
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT