Python uses indents to connect paths

The code inside the if and else statements is indented. This isn't just to make the code pretty. In Python, indents matter. Let's consider a different piece of example code: something that will decide if you can drive downtown. Python uses indents to connect a sequence of commands together to form paths.

print("You can drive downtown.") else:

print("Sorry")

print("You don't have enough fuel") print("What's next?")

This Cowhand is not on "the FALSE path because it * not indented- So it will always run.

So how do you connect branches together? You simply indent the second branch in by one more level.

This is the TRUE path.

This is the

FALSE path.

ThiSsee°»d if branch

V f°Tecied the fak: PWthe branch.

print("You can drive downtown.") else:

print("You should buy some gas.") else:

print("You better stay at home.") print("What's next?")

Hoi>te the extra

Indents matter in Python.

Be careful how you indent code in Python; if you don't indent your code correctly, your code might do something wildly different from what you expect.

You should now have enough information to go fix the code, but before we do that, let's take a look at how IDLE helps you indent code.

IDLE ...ata glance

You'll be using IDLE to enter all of the Python code in this book, so it's worth taking a little time to familiarize yourself with a few of its features.

Even though IDLE looks like a simple editor, it's actually packed full of smarts that will make Python programming much easier and faster for you. It's worth spending some time exploring IDLE's menus and help system, but for now here's a few handy hints to help you feel at home.

W.t ENTER, ,. ¡^en-ting by ¿Hiking on th, indent the ne*t l»«e it's called Preferent

The Help men« not «Ay oaves you help about IDLE, wt Vt also lets you read the built-mP^h^

documentation-

Eife Edit Format Jiun Options Windows

print "Ycij ca.~ drive downtown."

mnn^y > 10: pi i nt ( V on should buj £ g e r . " 1

Help

/S print ("You better e = 1 I " What' 3 .TCK-. : " )|

Before you type in "else:", make sure you hit BACKSPACE to move the indent back one leve|.

If you press TAB, IDLE Converts it into SPACE Characters.

If you press TAB, IDLE Converts it into SPACE Characters.

Poo] Puzzje

Your task is to take the Python code fragments from the pool and place them into the blank lines in the game. You may not use the same code fragment more than once, and you won't need to use all the code fragments. Your goal is to complete the guessing game program.

Hint: Don't forget to indent.

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

Note: each code

Note: each code

indent or else

Poo] Puzzje Solution

Your task was to take the Python code fragments from the pool and place them into the blank lines in the game. You could not use the same code fragment more than once, and you didn't need to use all the code fragments. Your goal was to complete the guessing game program.

Hint: Don't forget to indent.

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

print("You win!")

else:

print("Too high") else:

print("Too low") print("Game over!")

This piete of ¿ode fv-owi -the f ire of this pvogv, longev heeded vevsioh vevsioh

This piete of ¿ode fv-owi -the f ire of this pvogv, longev heeded

Tqst DRIVQ

So, what happens if you run the new version of the program?

Let's try a few tests. Remember, you will need to switch back to the program window for each run and choose Run module from the menu.

Python Shell

Hie fcdit bheji uebug uptions windows

Heip

RESTART

Python 3.0.1 (r30I:69556, Fab 17 2009, 15sl5:57| [GCC 4.3.2] oil linux2

Type "copyright", "oredita" or "license^)" for more information. ================================ RKRTM5T ===================

We 1 fTmriR I

■!> -"■t- ■:■.■■'* ■ Looks like the first guess was too low, so we need to try again.

Game overi

Welcome L

Gues3 the number Too high Game over J! >» ============:

Gues3 the nun^er: 5 ^ Weve guessed the

Game over!

j Now the guess is too high. : 7 Let's have another go, and...

RESTART

The program works! But, are the users any happier?

another guess ffll

The users still don't like it.

The program works, and now generates extra feedback, but there's a problem. If the users want to have another guess, they have to run the program again. They really want the program to keep asking them for another guess until they finally get the correct answer.

Can you see what the problem is?

How do we get the computer to do something repeatedly? Should we just make a copy of the code and paste it at the end of the file? That would make sure the user is asked twice. But what if they need to make 3 guesses? Or 4 guesses? Or 10,000 guesses?

What about the case where the guess is correct?

The guessing game program needs to be able to run some code repeatedly.

Was this article helpful?

0 0

Post a comment