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 you please tell me if this code needs to be fixed, if it does can...
Can you please tell me if this code needs to be fixed, if it does can you please post the fixed code below please and thank you /** * Models a Whole Life Policy. * * @author Tina Comston * @version Fall 2019 */ public class WholeLifePolicy { // instance variables    private double faceValue; // your code here - code the remaining instance field // constants /** * surrender rate. */ public static final double SURRENDER_RATE = .65; /**...
Can you please tell me if this code needs to be fixed, if it does can...
Can you please tell me if this code needs to be fixed, if it does can you please post the fixed code below please and thank you /** * Driver to demonstrate WholeLifePolicy class. * * @author Tina Comston * @version Fall 2019 */ public class WholeLifePolicyDriver { /** * Creates WholeLifePolicy object, calls methods, displays values. * */ public static void main() { WholeLifePolicyDriver myDriver = new WholeLifePolicyDriver(); // create a policy WholeLifePolicy policy = new WholeLifePolicy("WLP1234567", 50000, 20);...
I was wondering if you can tell me if the following code is correct and if...
I was wondering if you can tell me if the following code is correct and if its not can it be fixed so it does not have any syntax errors. Client one /** * Maintains information on an insurance client. * * @author Doyt Perry/<add your name here> * @version Fall 2019 */ public class Client { // instance variables private String lastName; private String firstName; private int age; private int height; private int weight; /** * First constructor for...
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 take a look and tell me what I have wrong here. VS 2019 .net...
Can someone take a look and tell me what I have wrong here. VS 2019 .net framework console app c## using System; using System.Collections.Generic; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; namespace dropbox10 { class MainClass { public static void Main(string[] args) { //create a list of employees List<Employee> allEmployees = new List<Employee>(); //create two full time employees FullTimeEmployee fe1 = new FullTimeEmployee("111", "Alice", 67888.00m); FullTimeEmployee fe2 = new FullTimeEmployee("222", "Bob", 67555.00m); //create two part time employees PartTimeEmployee pe1...
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++){...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT