Cherreads

Chapter 12 - Chapter 12 — Talking to the User

The screen was still glowing softly.

Pran sat in front of the computer, watching the blinking cursor.

_

Just a few minutes earlier, he had written his first Python program.

print("Hello, World!")

And it worked.

Simple.

Clean.

No extra setup.

The robot on the screen appeared again.

[^_^]

/| |\

/ \

Below it, the system displayed a new message.

PYTHON MODULE PROGRESS: 10%

NEXT TASK: USER INTERACTION

Pran smiled.

"That sounds familiar."

In the C programming module, the computer had asked him questions using scanf.

But Python had something simpler.

The screen displayed a line of code.

name = input("What is your name? ")

Pran read it slowly.

"Input…"

The robot explained through a system message.

INPUT() ALLOWS THE PROGRAM TO RECEIVE INFORMATION FROM THE USER

That meant the computer could ask questions and wait for an answer.

Pran typed the program exactly as shown.

name = input("What is your name? ")

Then he added another line.

print("Hello", name)

The full program now looked like this:

name = input("What is your name? ")

print("Hello", name)

He ran the program.

The computer asked:

What is your name?

Pran typed:

Pran

The screen responded instantly.

Hello Pran

Pran nodded.

"That was easy."

Let's understand what happened.

The line:

name = input("What is your name? ")

does three things.

First, the computer prints a message.

What is your name?

Second, the program waits for the user to type something.

Third, whatever the user types is stored inside the variable name.

So if the user types:

Alex

The variable becomes:

name = "Alex"

Then the program prints it.

print("Hello", name)

Which produces:

Hello Alex

Pran decided to experiment.

He ran the program again.

What is your name?

This time he typed:

Robot

The output appeared.

Hello Robot

Pran laughed.

"Okay, that's fun."

The robot nodded again.

[^_^]

Then the computer displayed another message.

USER INTERACTION VERIFIED

Another line appeared.

NEXT TASK: MULTIPLE QUESTIONS

Pran leaned forward.

"Multiple questions?"

The system showed another example.

name = input("What is your name? ")

city = input("Where do you live? ")

print("Hello", name)

print("You live in", city)

Pran typed it into the editor.

He ran the program.

The screen asked:

What is your name?

He typed:

Pran

Then another question appeared.

Where do you live?

He typed:

History

The program printed:

Hello Pran

You live in History

Pran nodded.

"So the computer can remember multiple things."

Exactly.

Each variable stores its own piece of information.

For example:

name → Pran

city → History

Variables are like labeled boxes.

Each box holds a value.

The robot moved slightly again.

[^o^]

Another message appeared.

PYTHON MODULE PROGRESS: 20%

Pran crossed his arms.

"Twenty percent already."

The computer displayed another message.

NEXT MODULE: NUMBERS AND CALCULATIONS

Pran smiled.

"That sounds interesting."

He had used numbers before in C.

But Python had its own way of working with numbers.

The cursor blinked again.

_

Waiting.

Silent.

Ready for the next command.

Pran cracked his knuckles.

"Alright Python," he said.

"Let's do some math."

Chapter 13 — Numbers and Calculations

You will learn:

• integers• decimal numbers• addition, subtraction, multiplication• simple Python math programs

More Chapters