Chapter 4: Communicating with the Command Center (Print Statements and User Input)

As an aspiring Python space explorer, you’ve prepared well. You’ve learned why Python is the language of choice for your space adventure and set up your Google Colab environment. You’ve learned about variables, data types, lists, and tuples. You’ve even started to pack your spacecraft and determine its specifications! Now, it’s time to establish a line of communication with your command center.

In this chapter, we’ll focus on two fundamental aspects of Python: print statements and user input. Both of these will play a crucial role in interacting with your program. Let’s dive in!

Throughout your space journey, you’ll need to report back to your command center frequently, sharing details about your spacecraft’s status, your current location, the conditions in space, and more. In Python, we use the print() function to display messages.

Using the print() Function

The print() function is perhaps the simplest yet one of the most used functions in Python. It outputs text to the console (in our case, the cell output in Google Colab). Here’s how you use it:

1 print("Hello, Command Center!")

If you run this code in a cell in Google Colab, you’ll see the message “Hello, Command Center!” appear in the output beneath the cell.

You can print multiple items in a single print statement by separating them with commas. Python will automatically add a space between each item:

1 location = "Mars"
2 print("We have arrived at", location)

This will output: “We have arrived at Mars”

The Importance of Print Statements in Debugging

Print statements are also incredibly useful for debugging. Debugging is the process of finding and fixing errors in your program. By strategically placing print statements in your code, you can observe the flow of your program and the values of your variables at various stages, which can help you identify where things are going wrong.

For example, if you’re unsure whether a particular section of your code is running, you could add a print statement there:

1 print("This part of the code is running!")

If you see this message in the output, you know that section of the code is indeed running.

User Input: Receiving Instructions from Command Center

As an explorer in space, you’ll not only need to report back to your command center, but also receive instructions from them. In Python, we can get input from the user (in our case, the command center) using the input() function.

Using the input() Function

The input() function presents a prompt to the user (the text inside the parentheses) and then waits for the user to type something and press Enter. The text typed by the user is returned by the function as a string.

Here’s a simple example:

1 name = input("Please enter your name: ")
2 print("Hello, ", name)

When you run this code, you’ll see the prompt “Please enter your name: “ appear. You can then type your name and press Enter. The program will then greet you by your name.

Prompting for User Input

When using the input() function, it’s essential to provide a clear and concise prompt that tells the user what they should enter. This prompt is often a question or a statement ending with a colon (:), but you can make it whatever you want.

It’s also important to remember that the input() function always returns a string. If you want to use the input as a number, you’ll need to convert it using the int() function (for integers) or the float() function (for floating-point numbers):

1 age = input("Please enter your age: ")
2 age = int(age)  # Convert the age to an integer
3 print("In one year, you will be", age + 1)

In this example, we ask the user for their age, convert it to an integer, and then use it in a calculation.

Project: A Conversation with the Command Center

Now that we’ve learned about print statements and user input, it’s time to put our knowledge to use in a new project. Your task is to write a program that simulates a conversation between you (the space explorer) and your command center.

The program should ask the user for various pieces of information, such as their name, their role in the mission, and their current location. It should then use this information to print out several messages that form a coherent conversation.

Here’s some example code to get you started:

 1 # Get information from the user
 2 name = input("Please enter your name: ")
 3 role = input("Please enter your role in the mission: ")
 4 location = input("Please enter your current location: ")
 5 
 6 # Print out a conversation
 7 print("Command Center: Hello, ", name)
 8 print(name, ": Hello, Command Center!")
 9 print("Command Center: We see that you're currently at", location)
10 print(name, ": That's correct, Command Center. I'm currently working as the", role, \
11 "at", location)

Feel free to be creative and expand upon this conversation! Remember, the goal is to practice using print statements and user input.

In the next chapter, we’ll learn how to handle situations that require decisions, using conditional statements. Until then, keep the lines of communication with your command center open and clear!