Programming lets you do more

You've got problems to solve and work to do, but your existing software doesn't quite cut it. Even with all those programs on your computer, you still need to do something different, something specific to you.

You've got problems to solve and work to do, but your existing software doesn't quite cut it. Even with all those programs on your computer, you still need to do something different, something specific to you.

You want to do more with your computer. You want to take control.

Learning to program gives you the power to create and solve. Learning to program puts you in charge.

But, how does programming work?

You want to do more with your computer. You want to take control.

Learning to program gives you the power to create and solve. Learning to program puts you in charge.

But, how does programming work?

Let's look at a simple game written in Python.

This code is a guessing-game program. Study it carefully, and opposite each line of code in the program, write down what you think the code does. If you're not sure what a particular line of code does, don't worry, but try to guess anyway. One line has already been entered to get you started:

print("Welcome!") g = input("Guess the number: ") guess = int(g) if guess == 5:

print("You lose!") print("Game over!")

Convert the input to a number.

tode written in release 3 ihe Pyiu prog^^g |a ej wh|ih

« ^ed throughout this book

Jharpen your pencil ^^ Solution print("Welcome!") g = input("Guess the number: ") guess = int(g) if guess == 5:

print("You lose!") print("Game over!")

This code is a guessing-game program. You were to write down what you think the code does.

rwt worry if your answers are d.+Wt£om ours. If they are ^

Display a welcome message.

Ask the user to input a guess. Convert the input to a number. Was the guessed number equal to 5? Tell the user "You win'" Otherwise...

End the program.

But what are g and guess?

You might be wondering what g and guess are in the code. They are called variables and they're used to keep track of data in the computer's memory.

tails Vt Vss '

Be careful with = signs in code.

A variable is really just a label for data. So if the user inputs "3" at the keyboard, then guess will be set to the number 3, and whenever the computer reads guess, it will read it as the value 3.

Wafcli WT Programming W languages use =

signs for different purposes. In most languages

(including Python), a double equals (==) is a test for equality.

It means, "are these two things equal?" In contrast, a single equal (=) is an instruction

(known as assignment) that means "set the value to."

Was this article helpful?

0 0

Post a comment