A little game with old Ziko python _python

Source: Internet
Author: User
Tags shuffle in python

When it comes to the list, it's about the game, and then it's never going to last. Not forget, is in what stage to do the most suitable. After a period of study, reader is not pure white, already belongs to the Python beginner. Now is the time to start the game.

Game content: Guess the number of games

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

Description of the game process

The program runs and randomly selects an integer within a range.
Prompts the user to enter a number, that is, guess the number that the program chooses immediately.
The program compares the number entered by the user to its own selection, and the user completes the game, or continues to guess.
Users who use fewer times prevail.
Analysis

In any form of program development, whether large or small, should be analyzed. That is, according to functional requirements, the different function points are decomposed. thereby determining the development process. We are doing a very small program now, and that's the way to do it.

Randomly select a number

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

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

Copy 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.

Look at the document patiently and see how it is used. However, the main things are listed, but it is still recommended that reader see the document in interactive mode with help before looking at the use of each function.

Random integer:

Copy Code code as follows:

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

Randomly select an even number between 0 and 100:

Copy Code code as follows:

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

Random floating-point numbers:

Copy Code code as follows:

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

Random characters:

Copy Code code as follows:

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

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

Copy Code code as follows:

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

Random Pick String:

Copy Code code as follows:

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

Shuffle: to disrupt the original order, in random order

Copy 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 the Random.randint () can be used. More come out is to buy one to get one to send a (oh. Forgotten, no one to buy, this course is all white to send.

One of the key technical points has been breached. can be programmed. Comb the process again. Draw a diagram to show:

(Note: Here I first lazy, reader can draw the program flowchart?) Especially if it is a beginner, the flowchart must draw its own OH. Just saw a friend on the internet said that they learn programming, but logical thinking is poor, so did not learn. Actually, drawing a flowchart is a good way to help improve your logical thinking. )

The picture is good, according to the intuitive understanding, the following code is a beginner often writes out (the old birds do not spray, because is represents the beginner's).

Copy 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 right, this number is:"
Print number
Else
Print "was wrong. "

The above procedure has been able to basically go through, but there are many flaws.

The most obvious is that only let people guess once, not many times. How to revise, can guess many times? After the use of the brain to see the code, or reader in their own code to change, can realize many guesses?

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

Based on the above revised ideas, the new code is as follows:

Copy 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 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 "was wrong. "

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

Yes. It's going to take a new thing: loops. If reader impatient, you can Google a while or for the loop, to further improve the game, if not hurry, can wait, then I will also talk about this part.

The game is not finished, that is, the use of the loop, 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.