CSE 110: Introduction to Programming

Learning Activity (1 of 2): Your First Program

The Basics of Writing a Program

We all interact with many kinds of devices, websites, and apps that are made from "programs." As you might imagine, apps such as Instagram or Google Maps are quite complex and have taken teams of professional programmers many months or years to create. You are starting on the journey that can lead to creating programs that are just polished and interactive, but we will begin with much simpler programs.

The essence of all programs, no matter how complex or simple, is that they provide a step-by-step list of instructions that the computer will follow. The computer, while powerful, is also very simplistic—it only does what you tell it. In addition, it does exactly what you tell it to do, nothing more and nothing less, and if you leave out a very small thing, or make even a tiny mistake, the computer is very unforgiving.

Starting with this activity, you are going to practice writing programs—the step-by-step instructions for the computer—and you are going to start with simple ones. You will start by displaying words on the screen and have the user type responses in a text window. While not nearly as glamorous as the professional apps you see around you, the fundamentals are the same. These simple programs will help you develop skills you need to create more powerful and more exciting applications as you continue to learn.

Hint from Instructor:

In programming, when we use the word "print," it means that the words will be displayed on the screen when the program is run—it doesn't have anything to do with actual "printers."

Please watch the following videos that introduce the idea of writing programs. While you are watching, you might want to pause the videos and follow along by typing the same code on your computer. The demos they walk through will be very similar to the assignments you have this week.

Hint from Instructor:

If you have trouble running your programs by typing python helloworld.py the way they do in the videos or finding the bottom section of the screen where they typed it, you might try selecting the green play button in the top right corner of Visual Studio Code instead.

Also, when you run your program for the first time, you will see lots of other text in the window that displays messages from when Visual Studio Code started up. You don't need to worry about all of these messages. The important thing is to look at the bottom and see if your message displayed after all of this other text.

Typing python and the name of your program to run it (like they do in the video) only works if your computer is set up to find Python automatically. If it works for you, feel free to use that approach, but otherwise, it's not a problem.

If typing python doesn't work for you, the simplest way to run your program, and the one that works the most reliably regardless of your computer's configuration, is to make sure your program is saved with .py at the end of the filename, and then click the green play button in the upper right hand corner to run the program. Other students have also found that typing python3 or py instead of python has worked for their configuration.

Activity Instructions

For this activity you will write a program that uses both input (obtaining data from the user via the keyboard) and output (displaying data to the user on the screen).

Instructions

Write a program that asks a user for their favorite color, then allow them to type in their color. Finally, have the program respond to them by displaying the text "Your favorite color is" followed by the color they typed.

In the following example, the user types in "Blue" for their favorite color:


Please type your favorite color: Blue
Your favorite color is
Blue

In this example, the user types "Hot Pink" for their favorite color:


Please type your favorite color: Hot Pink
Your favorite color is
Hot Pink

Notice that the program displays back the color that the user entered, so it is different each time, depending on the color that was typed.

To make this program work, you will need to get input from the user and then save the data they provide into a variable. Then, at the appropriate time you print (i.e. display) the data stored in that variable.

Sample Solution

When your program is finished, please view a sample solution to this program to compare your approach to that one.

Submission

When you have completed all of the learning activities for this week, you will return to Canvas and submit the associated quiz there.

Up Next

Other Links: