User manual
14
Python Flashcards
Python is the ideal programming language to introduce you to programming. It takes a bit to get used to
the syntax and the layout rules. A brief description of the main syntax elements of the Python
programming language in the form of a "cheat sheet” will help you with everyday programming. These
are based on the Python Flashcards by David Whale. Find out what this is all about on
bit.ly/pythonflashcards
. These Flash Cards do not explain the technical background, but describe the
syntax by giving very brief examples, telling you how exactly something is done.
1.4.1 Guessing numbers with Python
Rather than lingering on programming theory, algorithms and data types, let us write the very first small
game in Python; a simple guessing game where the player tries to guess a random number, which is chosen
by the computer, in as few attempts as possible.
1. Select in the Python shell’s menu
File/New Window
. This opens up a new window in which you type the
following programming code:
import random
number = random.randrange(1000); guess = 0; i = 0
while tip != number:
tip = input(“Your guess:”)
if number < guess:
print “The number sought is less than “,guess
if number > guess:
print “The number sought is greater than “,guess
i += 1
print “You guessed the number in ",i,". Guesses”
2. Save the file with
File/Save As
as spiel1.py. Or download the ready program file from
www.buch.cd
and open it in the Python shell using
File/Open
. The color coding shows up in the source code
automatically and helps you to find typos.
3. Before you start the game, note a special feature of the German language, namely the umlauts.. Python
runs on various computing platforms which encode umlauts differently. To display these correctly, select
Options/Configure IDLE
in the menu and in the tab
General
select the option
Locale-defined
in the field
Default Source Encoding
.