Design a classic game and a Python classic game with python

Source: Internet
Author: User

Design a classic game and a Python classic game with python

This is the 9th article on Python, which describes how to use Python to design a classic game: Guess the size.

In this game, we will use all the content I mentioned earlier: usage of variables, parameter transfer, function design, conditional control, and loop. Let's make a summary and review.

Game rules:

The initial principal amount is 1000 RMB. The default odds are doubled. If you win, you will get a factor of the amount, lose, and deduct a factor of the amount.

The program running result is as follows:

Now let's sort out our ideas.

After sorting out the ideas, we will start coding.

Shake the dice:

Define the roll_dice function. For three dice, the number of rounds numbers is 3, and the initial points value of the dice points is null. Here, the parameter transfer uses the previous keyword parameter transfer.

The random number is generated using import random. The most convenient thing in Python is that there are a lot of powerful library support. Now we can directly import a random built-in library and use it to generate random numbers. For example:

1 import random2 point = random. randrange () 3 # random. randrange () generate a random number from 1 to 6 4 print (point)

After print (point), you can see the printed random number. Each running result is random.

Next, let's take a look at the complete code for rolling the dice:

1 import random 2 3 def roll_dice (numbers = 3, points = None): 4 print ('----- shake dice -----') 5 if points is None: 6 points = [] 7 # points is an empty list. You can insert a new value to this list. 8 while numbers> 0: 9 point = random. randrange (1, 7) 10 points. append (point) 11 # Use the append () method to insert the point value to 12 numbers = numbers-113 # In the points list. Complete this operation once, and reduce numbers by 1, if the value is less than or equal to 0, this loop 14 return points will not be executed.

Fixed size:

The Code is as follows:

1 def roll_result (total): 2 isBig = 11 <= total <= 183 isSmall = 3 <= total <= 104 if isBig: 5 return 'Big '6 elif isSmall: 7 return '小'

Play games:

The initial principal is 1000 yuan, with a default odds of one time. If you win the game, you will get a factor of the amount. If you lose, you will be deducted a factor of one. If the principal is 0, the game ends.

Def start_game (): your_money = 1000 while your_money> 0: print ('----- game start -----') choices = ['day', 'small'] # choices values are big and small, the user needs to enter either of them as correct your_choice = input ('place a bet, large or small: ') your_bet = input ('bet amount:') if your_choice in choices: points = roll_dice () # Call the roll_dice function total = sum (points) # sum as the sum, and add the results of the three dice youWin = your_choice = roll_result (total) if youWin: print ('dice points: ', points) print ('Congratulations, you have won {} RMB. You now have {} RMB '. format (your_bet, your_money + int (your_bet) # your_bet is a string format, which needs to be converted to the int type for calculation your_money = your_money + int (your_bet) # newest principal else: print ('dice points: ', points) print (' Sorry, you lost {} RMB. You now have {} RMB '. format (your_bet, your_money-int (your_bet) your_money = your_money-int (your_bet) else: print ('incorrect format, please input it again ') # If the input is not large or small in the choices list, the format is incorrect. else: print ('game termination') start_game ()

Here, we have completed the design of the three major parts of the game. You must think carefully, sort out the design ideas, and work out the code.

Finally, attach[Guess size] complete game code:

1 import random 2 3 def roll_dice (numbers = 3, points = None): 4 print ('----- shake dice -----') 5 if points is None: 6 points = [] 7 while numbers> 0: 8 point = random. randrange (1, 7) 9 points. append (point) 10 numbers = numbers-111 return points12 13 14 def roll_result (total ): 15 isBig = 11 <= total <= 1816 isSmall = 3 <= total <= 1017 if isBig: 18 return 'day' 19 elif isSmall: 20 return '小' 21 22 23 def start_game (): 24 your_money = 100025 while your_money> print ('----- start of game -----') 27 choices = ['da ', '小'] 28 your_choice = input ('place a bet, large or small: ') 29 your_bet = input ('bet amount:') 30 if your_choice in choices: 31 points = roll_dice () 32 total = sum (points) 33 youWin = your_choice = roll_result (total) 34 if youWin: 35 print ('dice points: ', points) 36 print ('Congratulations, you have won {} yuan, you now have {} yuan '. format (your_bet, your_money + int (your_bet) 37 your_money = your_money + int (your_bet) 38 else: 39 print ('dice points: ', points) 40 print ('Sorry, you lost {} RMB, you have {} RMB now '. format (your_bet, your_money-int (your_bet) 41 your_money = your_money-int (your_bet) 42 else: 43 print ('incorrect format, please input ') 44 else: 45 print ('game termination') 46 47 start_game ()

Operating Environment: Python version, 3.6; PyCharm version, 2016.2; Computer: Mac

----- End -----

Author: du wangdan, Public Account: du wangdan, Internet product manager.

Related Article

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.