Next - Introduction | Table of Contents | Next - #2 Temperature Conversion

Exercise #1: Hello, World!

print('Hello, world!')   Hello, world!

 “Hello, world!” is a common first program to write when learning any programming language. It makes the text “Hello, world!” appear on the screen. While not much of a program, it serves as a simple test for whether the programmer has the language interpreter correctly installed, the computer environment set up, and a basic understanding of how to write a complete program.

This exercise adds an additional step to collect keyboard input from the user. You’ll also need to concatenate (that is, join together) string values to greet the user by name. The exercises in this book are for those with some experience writing programs, so if this exercise is currently beyond your skill level, I’d recommend reading some of the beginner resources I discussed in the Prerequisites section of the Introduction.

Exercise Description

Write a program that, when run, greets the user by printing “Hello, world!” on the screen. Then it prints a message on the screen asking the user to enter their name. The program greets the user by name by printing the “Hello,” followed by the user’s name.

Let’s use “Alice” as the example name. Your program’s output should look like this:

Hello, world!

What is your name?

Alice

Hello, Alice

Try to write a solution based on the information in this description. If you still have trouble solving this exercise, read the Solution Design and Special Cases and Gotchas sections for additional hints.

Prerequisite concepts: print(), strings, string concatenation

Solution Design

There is no surprising design to the solution. It is a straightforward four-line program:

1.      Print the text, “Hello, world!”

2.      Print the text, “What is your name?”

3.      Let the user enter their name and store this in a variable.

4.      Print the text “Hello,” followed by their name.

The program calls Python’s print() function to display string values on the screen. As always, the quotes of the string aren’t displayed on the screen because those aren’t part of the string value’s text, but rather mark where the string begins and ends in the source code of the program. The program calls Python’s input() function to get the user input from the keyboard and stores this in a variable.

The + operator is used to add numeric values like integers or floating-point numbers together, but it can also create a new string value from the concatenation of two other string values.

Special Cases and Gotchas

Make sure that there is a space before printing the user’s name. If the user entered “Alice”, the program should print 'Hello, Alice' and not 'Hello,Alice'.

Now try to write a solution based on the information in the previous sections. If you still have trouble solving this exercise, read the Solution Template section for additional hints.

Solution Template

Try to first write a solution from scratch, But if you have difficulty, you can use the following partial program as a starting place. Copy the following code from https://invpy.com/helloworld-template.py and paste it into your code editor. Replace the underscores with code to make a working program:

# Print "Hello, world!" on the screen:

____('Hello, world!')

# Ask the user for their name:

____('What is your name?')

# Get the user's name from their keyboard input:

name = ____()

# Greet the user by their name:

print('Hello, ' + ____)

The complete solution for this exercise is given in Appendix A and https://invpy.com/helloworld.py. You can view each step of this program as it runs under a debugger at https://invpy.com/helloworld-debug/.

Further Reading

A “Hello, world!” program is a good target program to create when learning a new programming language. Most languages have a concept of standard streams for text-based, command-line programs. These programs have a stream of input text and a stream of output text. The stream of output text the program produces appears on the screen (via print() in Python). The stream of input text the program accepts comes from the keyboard (via input() in Python). The main benefit of streams is that you can redirect them: the text output can go to a file instead of the screen, or a program’s input can come from the output of another program instead of the keyboard. You can learn more about the command-line interface, also called terminal windows, in the free book “Beyond the Basic Stuff with Python” at https://inventwithpython.com/beyond/. Command-line programs tend to be simpler than graphical programs, and you can find nearly a hundred such programs in the free book, The Big Book of Small Python Projects at https://inventwithpython.com/bigbookpython/.

Next - Introduction | Table of Contents | Next - #2 Temperature Conversion