Make a little game with the old Ziko python

Source: Internet
Author: User
Tags shuffle
When it comes to the list, it's a matter of playing games, and it's never been followed. Not forget, is in which stage to do the most suitable. After a period of learning, crossing is not purely small white, already belongs to the Python beginner. Now is the time to start playing that game.

Game content: Guess the number game

It's too easy. Yes, the game is not very difficult, but the game contains something that is worth pondering.

Game Process Description

The program runs and randomly selects an integer within a range.
Prompts the user to enter a number, which is the number that the program immediately selected.
The program compares the number entered by the user with his or her selection, and the user finishes the game, or continues to guess.
Users who have fewer uses wins.
Analysis

Before any form of program development, whether large or small, will be analyzed. That is, according to functional requirements, different functional points are decomposed. So that the development process is determined. We are now doing a very small program, and that is the way to do it.

Randomly select a number

To implement a random selection of a number, you can use a random function in Python: random. The following is a brief introduction to this function, in addition to the application, but also to expand the point, perhaps elsewhere crossing can be used.

Or to first strengthen a learning method, is to learn to view the Help document.

Copy the Code code as follows:


>>> import Random #这个是必须的 because it is not a 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 in module random:

Randint (self, A, B) method of random. Random instance
Return random integer in range [A, b], including both end points.

Read the document patiently and understand how it is used. However, the main thing is listed, but it is still recommended that crossing look at the document through Help in interactive mode before looking at the use of each function.

Random integers:

Copy the Code code as follows:


>>> Import Random
>>> Random.randint (0,99)
21st

Randomly select even numbers between 0 and 100:

Copy the Code code as follows:


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

Random floating point number:

Copy the Code code as follows:


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

Random characters:

Copy the Code code as follows:


>>> Import Random
>>> random.choice (' Qiwsir.github.io ')
' G '

Select a specific number of characters in more than one character:

Copy the Code code as follows:


>>> Import Random
Random.sample (' Qiwsir.github.io ', 3)
[' W ', ' s ', ' B ']

Randomly pick a string:

Copy the Code code as follows:


>>> Import Random
>>> random.choice ([' Apple ', ' pear ', ' peach ', ' orange ', ' Lemon '])
' Lemon '

Shuffle: The original order is scrambled, in random order

Copy the Code code as follows:


>>> Import Random
>>> items = [1, 2, 3, 4, 5, 6]
>>> random.shuffle (items)
>>> Items
[3, 2, 5, 6, 4, 1]

A little too much. However, in this experiment, the value of Random.randint () can be used. More out is to buy one get one (oh. Forget, no one buys, this course is all white send.

One of the key technology points has been breached. It can be programmed. Then comb the process. Draw a diagram showing:

(Note: Here I first lazy, crossing can draw the flowchart of this program?) Especially if it is a beginner, the flowchart must be painted yourself oh. Just saw a friend on the internet said that they learn programming, but logical thinking is poor, so did not learn. In fact, drawing a flowchart is a good way to help improve logical thinking, please draw. )

The picture is good, according to the intuitive understanding, the following code is a beginner often write out (the birds do not spray, because it is representative of beginners).

Copy the Code code as follows:


#!/usr/bin/env python
#coding: Utf-8

Import Random

Number = Random.randint (1,100)

Print "Please enter a natural number within 100:"

Input_number = Raw_input ()

If number = = Int (input_number):
Print "Guess the right, this number is:"
Print number
Else
Print "wrong. "

The above program has been able to basically walk through, but there are many flaws.

The most obvious is only to let people guess once, not many times. How to modify, can you guess many times? After thinking about the code, or crossing in their own code to change, can you achieve multiple guesses?

In addition, can you enhance some friendliness, let the user know that their input number is large, or small.

Based on the above modification idea, the new code is as follows:

Copy the Code code as follows:


#!/usr/bin/env python
#coding: Utf-8

Import Random

Number = Random.randint (1,100)

Print "Please enter a natural number within 100:"

Input_number = Raw_input ()

If number = = Int (input_number):
Print "Guess the right, this number is:"
Print number
elif number > Int (input_number):
print "Small"
Input_number = Raw_input ()
Elif number < int (input_number):
Print "big."
Input_number = Raw_input ()
Else
Print "wrong. "

Well, it seems to be a little bit more progressive because the user is allowed to enter the second time. It also tells the user whether the input is large or small. But that's not going to work. You should be able to enter it many times until it is correct.

Yes. This is going to use a new thing: the loop. If crossing impatient, can Google a while or for loop, to further improve this game, if not anxious, can wait, then I will talk about this part.

This game is not finished yet, that is, the loop is used, and the following will continue.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.