Check50 cs50/problems 2022 python indoor

Indoor Voice

WRITING IN ALL CAPS IS LIKE YELLING.

Best to use your “indoor voice” sometimes, writing entirely in lowercase.

In a file called indoor.py, implement a program in Python that prompts the user for input and then outputs that same input in lowercase. Punctuation and whitespace should be outputted unchanged. You’re welcome, but not required, to prompt the user explicitly, as by passing a str of your own as an argument to input.

Hints
  • Recall that input returns a str, per docs.python.org/3/library/functions.html#input.
  • Recall that a str comes with quite a few methods, per docs.python.org/3/library/stdtypes.html#string-methods.

Before You Begin

Execute cd by itself in your terminal window. You should find that your terminal window’s prompt resembles the below:

Next execute

to make a folder called indoor in your codespace.

Then execute

to change directories into that folder. You should now see your terminal prompt as indoor/ $. You can now execute

to make a file called indoor.py where you’ll write your program.

Demo

How to Test

Here’s how to test your code manually. At the indoor/ $ prompt in your terminal: :

  • Run your program with python indoor.py. Type HELLO and press Enter. Your program should output hello.
  • Run your program with python indoor.py. Type THIS IS CS50 and press Enter. Your program should output this is cs50.
  • Run your program with python indoor.py. Type 50 and press Enter. Your program should output 50.

If you run into an error saying your file cannot be opened, retrace your steps to be sure that you are inside your indoor folder and have saved your indoor.py file there. Remember how?

You can execute the below to check your code using check50, a program that CS50 will use to test your code when you submit. But be sure to test it yourself as well!

check50 cs50/problems/2022/python/indoor

Green smilies mean your program has passed a test! Red frownies will indicate your program output something unexpected. Visit the URL that check50 outputs to see the input check50 handed to your program, what output it expected, and what output your program actually gave.

How to Submit

In your terminal, execute the below to submit your work.

submit50 cs50/problems/2022/python/indoor

Getting Started

Recall that Visual Studio Code (aka VS Code) is a popular “integrated development environment” (IDE) via which you can write code. So that you don’t have to download, install, and configure your own copy of VS Code, we’ll use a cloud-based version instead that has everything you’ll need pre-installed.

Log into code.cs50.io using your GitHub account. Once your “codespace” loads, you should see that, by default, VS Code is divided into three regions. Toward the top of VS Code is your “text editor”, where you’ll write all of your programs. Toward the bottom of is a “terminal window”, a command-line interface (CLI) that allows you to explore your codespace’s files and directories (aka folders), compile code, and run programs. And on the left is your file “explorer,” a graphical user interface (GUI) via which you can also explore your codespace’s files and directories.

Start by clicking inside your terminal window, then execute cd by itself. You should find that its “prompt” resembles the below.

Click inside of that terminal window and then type

followed by Enter in order to make a directory called hello in your codespace. Take care not to overlook the space between mkdir and hello or any other character for that matter!

Here on out, to execute (i.e., run) a command means to type it into a terminal window and then hit Enter. Commands are “case-sensitive,” so be sure not to type in uppercase when you mean lowercase or vice versa.

Now execute

to move yourself into (i.e., open) that directory. Your prompt should now resemble the below.

If not, retrace your steps and see if you can determine where you went wrong!

Shall we have you write your first program? Execute

to create a new file called hello.c, which should open automatically in your codespace’s text editor. As soon as you save the file with command-S (on macOS) or control-S (on Windows), it should also appear in your codespace’s explorer.

Proceed to write your first program by typing precisely these lines into hello.c:

#include 

int main(void)
{
    printf("hello, world\n");
}

Notice how VS Code adds “syntax highlighting” (i.e., color) as you type, though VS Code’s choice of colors might differ from this problem set’s. Those colors aren’t actually saved inside of the file itself; they’re just added by VS Code to make certain syntax stand out. Had you not saved the file as hello.c from the start, VS Code wouldn’t know (per the filename’s extension) that you’re writing C code, in which case those colors would be absent.

Listing Files

Next, in your terminal window, immediately to the right of the prompt (hello/ $), execute

You should see just hello.c? That’s because you’ve just listed the files in your hello folder. In particular, you executed a command called ls, which is shorthand for “list.” (It’s such a frequently used command that its authors called it just ls to save keystrokes.) Make sense?

Compiling Programs

Now, before we can execute the hello.c program, recall that we must compile it with a compiler, translating it from source code into machine code (i.e., zeroes and ones). Execute the command below to do just that:

And then execute this one again:

This time, you should see not only hello.c but hello listed as well? You’ve now translated the source code in hello.c into machine code in hello.

Now execute the program itself by executing the below.

Hello, world, indeed!

Getting User Input

Suffice it to say, no matter how you compile or execute this program, it only ever prints hello, world. Let’s personalize it a bit, just as we did in class.

Modify this program in such a way that it first prompts the user for their name and then prints hello, so-and-so, where so-and-so is their actual name.

As before, be sure to compile your program with:

And be sure to execute your program, testing it a few times with different inputs, with:

Walkthrough

Here’s a “walkthrough” (i.e., tour) of this problem, if you’d like a verbal overview of what to do too!

Hints

Don’t recall how to prompt the user for their name?

Recall that you can use get_string as follows, storing its return value in a variable called name of type string.

string name = get_string("What's your name? ");

Don’t recall how to format a string?

Don’t recall how to join (i.e., concatenate) the user’s name with a greeting? Recall that you can use printf not only to print but to format a string (hence, the f in printf), a la the below, wherein name is a string.

printf("hello, %s\n", name);

Use of undeclared identifier?

Seeing the below, perhaps atop other errors?

error: use of undeclared identifier 'string'; did you mean 'stdin'?

Recall that, to use get_string, you need to include cs50.h (in which get_string is declared) atop a file, as with:

How to Test Your Code

Execute the below to evaluate the correctness of your code using check50, a command-line program that will output happy faces whenever your code passes CS50’s automated tests and sad faces whenever it doesn’t! But be sure to compile and test it yourself as well!

check50 cs50/problems/2022/x/hello

Execute the below to evaluate the style of your code using style50, a command-line program that will output additions (in green) and deletions (in red) that you should make to your program in order to improve its style. If you have trouble seeing those colors, style50 supports other modes too!

How to Submit

In your terminal, execute the below to submit your work.

submit50 cs50/problems/2022/x/hello