After a period of time to study, the reader is not the pure white, already belongs to python beginners. Now is the time to start the game. Play a game, but still have some basic knowledge first

when story about the list mentioned do game, then this thing has not succeeded. Not forget, is the most suitable wonder at what stage do. After a period of time to study, the reader is not the pure white, already belongs to python beginners. Now is the time to start the game.

content of the game: guess Numbers game

is too simple. Yes, the game difficulty is not great, but the game contains what is worth pondering.

the game process description

program to run, randomly choose an integer in a certain range.
Prompt the user for Numbers, that is, guess program and then choose the Numbers.
Program will user input of Numbers and their chosen contrast, as the user finish the game, or continue to guess.
Use fewer users.
Analysis of

before any form of program development, whether big or small, are analyzed. Namely according to the functional requirements, the different function decomposition. To determine the development process. We now do a small program, and to do so.

choose a random number

in order to realize random a number, you can use a random function in python: the random. Below the function are introduced briefly, except for the application and extension points, perhaps on the reader can use elsewhere.

still should first strengthen a method of learning is to learn to view the help documents.


code is as follows:


>>>The import random  #this is a must, because not built-in function
>>>Dir (random)
['BPF', 'LOG4', 'NV_MAGICCONST', 'RECIP_BPF', 'Random', 'SG_MAGICCONST', 'SystemRandom', 'TWOPI', 'WichmannHill', '_BuiltinMethodType', '_MethodType', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '_acos', '_ceil', '_cos', '_e', '_exp', '_hashlib', '_hexlify', '_inst', '_log', '_pi', '_random', '_sin', '_sqrt', '_test', '_test_generator', '_urandom', '_warn', 'betavariate', 'choice', 'division', 'expovariate', 'gammavariate', 'gauss', 'getrandbits', 'getstate', 'jumpahead', 'lognormvariate', 'normalvariate', 'paretovariate', 'randint', 'random', 'randrange', 'sample', 'seed', 'setstate', 'shuffle', 'triangular', 'uniform', 'vonmisesvariate', 'weibullvariate']

>>>Help (random. Randint)

Help on method randint module in the random:

randint (self, a, b) method of the random, random instance
      Return the random integer in the range [a, b], o both end points.

patiently to see the document, will understand how to use. Still, it's the main thing, but still suggested that before the reader see the use of each function, in interactive mode by help to check the document.

random integer:


code is as follows:


>>>Import the random
>>>The random randint (0 13)
21

a random number between 0 and 100:


code is as follows:


>>>Import the random
>>>Random. Randrange (0, 101, 2)
42

random floating point Numbers:


code is as follows:


>>>Import the random
>>>The random. The random ()
0.85415370477785668
>>>The random uniform (1, 10)
5.4221167969800881

random characters:


code is as follows:


>>>Import the random
>>>The random choice (' qiwsir. Making. IO)
'g'

more characters from a specific number of characters:


code is as follows:


>>>Import the random
The random sample (' qiwsir. Making. IO, 3)
[' w ', 's', 'b']

a random string:


code is as follows:


>>>Import the random
>>>The random choice ([' apple ', 'pear', 'peach', 'orange', 'lemon'])
'lemon'

shuffling: upset the order of the original, according to the random order


code is as follows:


>>>Import the random
>>>The items=[1, 2, 3, 4, 5, 6]
>>>The random shuffle (items)
>>>The items
[3, 2, 5, 6, 4, 1]

a little more. In this experiment, however, value with the help of random. The randint (). Comes out more is to buy one, get one free (oh. Forget that no one buy it, this course is all gratis).

one of the key technical points have breakthrough. Can be programmed. Combing the process again. Draw a diagram shows:

(note: let me first lazy here, the reader can draw the flow chart of the program? Especially if it is a beginner, flowchart must draw yourself oh. Just saw a friend online to say oneself to learn programming, but logic thinking is poor, so didn't learn it well. In fact, draw the flow chart is to help improve the logical thinking a good way, please draw a map.)

picture ok, according to the understanding of intuitive, the following code is a beginners often write (veteran who do not, for it is representing a beginner).


code is as follows:


#! The/usr/bin/env python
#coding: utf-8

import random

number=random. Randint (1100)

print "please enter a natural number within 100:"

input_number=raw_input ()

if number==int (input_number) :
      Print "guessed, this number is:
      Print the number
The else:
      Print "wrong."

the above procedures have been able to go through basic, but there are still many defects.

one of the most obvious is the only let people guess once, not many times. How to modify, can guess many times? Use your head after the code, or the reader change in your own code, can realize multiple guess?

in addition, can enhance some friendly, let the user know that their own input number is big, or small.

in accordance with the above change idea, the new code is as follows:


code is as follows:


#! The/usr/bin/env python
#coding: utf-8

import random

number=random. Randint (1100)

print "please enter a natural number within 100:"

input_number=raw_input ()

if number==int (input_number) :
      Print "guessed, this number is:
      Print the number
Elif number >Int (input_number) :
      Print "small"
      Input_number=raw_input ()
Elif number <Int (input_number) :
      Print "big"
      Input_number=raw_input ()
The else:
      Print "wrong."

well, it seems a little bit of progress than before, because the allows the user to enter a second time. At the same time also tell the user input is big or small. But, this also not line. Should be able to enter many times, until it is correct.

yes. This is about to use a new thing: cycle. If the reader impatient, you can Google it while or for loop, to further improve the game, if you don't try so hard, can wait, then I will talk about this part.

this game is not finished yet, which USES the circular, behind will continue.

you might be interested in the article:

  • follow old neat Python data types of the summary
  • with the old learning Python's variables and reference objects
  • with the old learning Python's assignment, simple is not simple
  • with the old learning Python's pit dad character encoding
  • with the old learning Python's normal to say a word
  • with the old learning Python's print,
  • the from of old together learn Python formatted expression to method


This concludes the body part