Placeholder Image

Subtitles section Play video

  • Hi everyone, my name is Giovanna Proença and today we're going to talk about Problem

  • Set Zero Playback Speed of CS50 Python.

  • So if you have any question about programming, if you want to know about more the career or other things, please check the description below where you can schedule a 30-minute free session with us, alright?

  • And before we start, I would like to emphasize that this video solution is made for those who have already completed the assignment and want to have a second view of the code, alright?

  • We do not encourage plagiarism.

  • So let's just start.

  • Here we have some people have a habit of speaking rather quickly and it'd be nice to slow them down.

  • So allow YouTube 0.75 playback speed or even by having them pause between words.

  • In a file called playback.py, implement a program in Python that prompts the user for input and then outputs the same input replacing each space with three periods.

  • So here we have the example we have here.

  • Once we run the code, we're going to write a message to user and we're going to prompt the message with three periods instead of the white space, okay?

  • So let's start thinking about the things that we need.

  • I already did here a pseudocode, alright?

  • So basically we need to get input from the user, then we're going to change white space for three dots and then we're going to print the output.

  • So how can we do the first test?

  • How can we get input from the user?

  • Let's see this explanation.

  • Basically the function input allows us to ask questions to the user and the answer that the user typed in, we can start in a variable.

  • For example, if we want to ask the name of the user, we can do username equals to input.

  • What's your name?

  • And it will be prompt in the terminal.

  • The user can write his name.

  • If the user types in Giovanna, the variable username will start Giovanna.

  • Since the answer is stored in a variable, we can use this answer in our code.

  • So like we saw, we can use the input function, alright?

  • So let's grab the message from the user.

  • So I'm going to create here a variable called msg to start a message and we're going to use input here, okay?

  • If you want, you can type something in here inside the input, but in our case, I won't type anything, alright?

  • So we're going to see here that if we do python playback.py, it's going to appear this cursor where we can type in this is CS50 and this message is stored in our code, okay?

  • If we print the output, it will print the same message.

  • If we do print a message, it will print this, this is CS50, okay?

  • It's printing the same thing.

  • But this is not the goal of our problem.

  • The goal of our problem is to change the whitespace for three periods.

  • So how can we do this?

  • We're going to use the replace function.

  • Let's understand how this replace function works.

  • The replace method replaces a specified phrase with another specified phrase.

  • If we will have a variable txt equals to I like bananas and we want to change bananas to apple, we can use the replace method to make this change for us.

  • We can do txt.replace and inside the parenthesis we put the word that we want to change, in our case bananas, with the new word that we want to replace, in our case apples, and our string txt will become I like apples.

  • So in our case, instead of replacing bananas to apples, we're going to replace whitespace for three dots, okay?

  • So let's create a new variable I'm going to call new message, alright?

  • And in here we're going to use the previous variable that was storing the message, so msg. and now we're going to use the method replace, okay?

  • So .replace and what we want to replace?

  • We have to pass two arguments, two parameters.

  • The first one will be the word or the character that we want to replace.

  • So in our case we want to replace whitespace.

  • So in here we're going to use quotation mark and click space in the middle, alright?

  • So we can signalize that we want to replace the whitespace.

  • And we want to replace by what?

  • So in here we're going to use three dots inside.

  • So this means that everything that is whitespace will become three dots, okay?

  • And finally we're going to print this new variable that we are storing the new message, okay?

  • So let's try it out.

  • So if we do python playback.py and if we type in this is cs50.

  • Now we have the three periods instead of the whitespaces, okay?

  • So this is it for the function.

  • Let's do check50 and see if it's working.

  • So if we take a look in here, we took all green.

  • So this means that our code is running fine, okay?

  • So this is it for the playback problem.

  • If you have any questions, please let us know, send us a message or a comment, subscribe to our channel, please give us a thumbs up and see you in the next video, alright?

  • Bye bye!

Hi everyone, my name is Giovanna Proença and today we're going to talk about Problem

Subtitles and vocabulary

Click the word to look it up Click the word to find further inforamtion about it