I want to make a small game with old Qi learning Python, and I want to learn python games.

Source: Internet
Author: User

I want to make a small game with old Qi learning Python, and I want to learn python games.

When talking about the list, I mentioned the game. Later, this was not a connection. Not forgetting, but at which stage you want to be the most appropriate. After a period of study, the reader is no longer a pure hacker, but a beginner in python. Now is the time to start that game.

Game content: Guess digital games

It's too easy. Yes, the game is not very difficult, but what it contains is worth playing.

Game process description

Run the program and select an integer in a random range.
Prompt the user to enter a number, that is, the number selected by the guess program.
The program compares the number entered by the user with the one selected by the user. The user completes the game, otherwise he will continue to guess.
Users who use less frequently win.
Analysis

Before any form of program development, both large and small must be analyzed. That is, different functional points are decomposed according to the functional requirements. To determine the development process. Now we are doing a very small program.

Select a random number

To randomly select a number, you can use a random function in python: random. Next, we will give a brief introduction to this function. In addition to this application, we will also expand the point where you may be able to access the function elsewhere.

To learn more, you must first learn how to view the help documentation.

Copy codeThe Code is as follows:
>>> Import random # This is required 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 _', '_ ACs',' _ ceil ',' _ cos ', '_ E',' _ exp ',' _ hashlib ',' _ hexlify ',' _ inst', '_ log',' _ pi ',' _ random ', '_ sin',' _ sqrt ',' _ test', '_ test_generator', '_ urandom', '_ Warn', 'betavariate', 'choice ', 'division ', 'expvariate', 'gammavariate', 'gauss', 'getrandbucket', 'getstate', 'jumpahead', 'lognormvariate', 'normalvariate', 'paretovariate ', 'randt', 'rand', 'randrange', 'sample', 'seed', 'setstate', 'shuffle', 'trigger', 'uniform', 'vonmisesvariate ', 'wisullvariate']

>>> 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 then understand how to use it. However, we recommend that you use help to view the document in interactive mode before reading every function.

Random INTEGER:

Copy codeThe Code is as follows:
>>> Import random
>>> Random. randint (0, 99)
21

Randomly select an even number between 0 and 100:

Copy codeThe Code is as follows:
>>> Import random
>>> Random. randrange (0,101, 2)
42

Random Floating Point Number:

Copy codeThe Code is as follows:
>>> Import random
>>> Random. random ()
0.85415370477785668
>>> Random. uniform (1, 10)
5.4221167969800881

Random characters:

Copy codeThe Code is as follows:
>>> Import random
>>> Random. choice ('qiwsir. github. io ')
'G'

Select a specific number of characters from multiple characters:

Copy codeThe Code is as follows:
>>> Import random
Random. sample ('qiwsir. github. io ', 3)
['W', 's', 'B']

Randomly selected string:

Copy codeThe Code is as follows:
>>> Import random
>>> Random. choice (['apple', 'pear ', 'peach', 'Orange ', 'lemon'])
'Limon'

Shuffling:Disrupt the original order and arrange it in random order

Copy codeThe Code is as follows:
>>> Import random
>>> Items = [1, 2, 3, 4, 5, 6]
>>> Random. shuffle (items)
>>> Items
[3, 2, 5, 6, 4, 1]

A little more. However, in this experiment, you can use random. randint. Buy one get one free (OH. If you forget it, no one will buy it. This course is offered in vain ).

One of the key technical points has been broken through. You can program it. Repeat the process. Draw a chart:

(Note: I will be lazy first. Can you draw a flowchart of this program? Especially if you are a beginner, you must draw the flowchart yourself. I saw a friend on the internet saying that he had learned programming, but his logic thinking was poor, so he didn't learn well. In fact, drawing a flowchart is a good way to help improve logical thinking .)

The picture is good. According to the intuitive understanding, the following code is often written by beginners (the old birds do not spray, because it is representative of beginners ).

Copy codeThe Code is as follows:
#! /Usr/bin/env python
# Coding: UTF-8

Import random

Number = random. randint (1,100)

Print "enter a natural number less than 100 :"

Input_number = raw_input ()

If number = int (input_number ):
Print "guessed it, the number is :"
Print number
Else:
Print "error. "

The above program has been able to basically go through, but there are still many defects.

The most obvious thing is that it can only be guessed once, not multiple times. How can I modify it? Can I make multiple guesses when I think about the code, or when I change my code?

In addition, can we enhance friendliness and let users know whether their input is large or small.

Based on the above modification ideas, the new Code is as follows:

Copy codeThe Code is as follows:
#! /Usr/bin/env python
# Coding: UTF-8

Import random

Number = random. randint (1,100)

Print "enter a natural number less than 100 :"

Input_number = raw_input ()

If number = int (input_number ):
Print "guessed it, the 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 "error. "

Well, it seems to be a little better than before, because the user is allowed to enter the second time. It also tells the user whether the input is large or small. However, this is not acceptable. You should be able to enter it multiple times until it is correct.

Yes. This requires a new thing: loop. If you are eager to watch the game, you can google the while or for loop to further improve the game. If you are not worried, you can wait and I will talk about it later.

This game is not complete yet, that is, it uses a loop and will continue later.


What preparations should I do if I want to create a small game using python?

E

I will use python to make a small game for you.

Yao Di
 

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.