Python implements digital bomb game

Python-digital bomb game, for your reference, the specific content is as follows

**Digital bomb game rules: **

Within a number range, there is a number as a bomb, and whoever guesses the bomb will be punished. For example, the range is 1~99,
The bomb is 60, and then guessed a number is 30, 30 is not a bomb, so now the range of guessing numbers is reduced to 30~100.
I guessed that the number 80, 80 is not a bomb, so now I have reduced the range to 30~80. Every time I guess I can’t guess the value on the boundary.
Until you or the computer guess the bomb, then you will be punished and the game is over

Requirement: You first enter a number if it is not a bomb, and then let the computer narrow the range and enter a number, if it is not a bomb, you narrow the range again and enter it again, then go to the computer again, and repeat until you find the bomb

O. Find the core idea first

1、 Generate bomb
2、 Print bomb range
3、 Guess it yourself
4、 Reduce bomb range
5、 Computer generated random number (computer guessed once)
6、 Continue to reduce the bomb range
7、 Repeat these operations until the bomb explodes and the game is over!

Code

import random
x =random.randint(1,100)print("Bomb number XX")
start =1#Define minimum range
end =100#Define the maximum range
while True:
 num =int(input("enter{}-{}Integer between:".format(start, end)))if num x:
 end = num
 print("You guessed it")
 elif num == x:print("You lose, the game is over!")break
 elif num < x:
 start = num
 print("You guess it&#39;s small")
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - computer(Xiao Hong)
 computer = random.randint(start,end)print("Xiaohong believes that the bomb is:",computer)if computer x:
 end = computer
 print("Xiaohong guessed it")
 elif computer == x:print("you win")print("game over")break
 elif computer<x:
 start=computer
 print("Xiaohong guessed too small")

The core code uses a comparison function to update the bomb range

Use random to generate random numbers in (1-100) (representing bombs)
Use two variables to define the upper and lower limits of the bomb
Use input to accept the number you guessed, use if to determine if the number you guess is greater than the bomb, assign the number you guessed to the bomb upper limit end (change the bomb upper limit), if it is equal to the bomb, the game is over! If it is less than the bomb, assign the value you guessed to the bomb offline start
Use random to generate a random number, let Xiaohong guess once, and also use if to determine the upper limit of the bomb value change, which is less than the lower limit of the reduction. Until the game is over!

================== Operation result ==================

More interesting classic mini game implementation topics, share with you:

C++ classic games summary

Python classic games summary

python tetris game collection

JavaScript classic games are constantly playing

Summary of classic java games

JavaScript classic games summary

The above is the whole content of this article, I hope it will be helpful to everyone's study.

Recommended Posts

Python implements digital bomb game
Python implements digital bomb game program
Python implements tic-tac-toe game
Python implements Tetris game
Python implements minesweeper game
Python implements guessing game
Python implements WeChat airplane game
Python implements word guessing game
Python implements a guessing game
Python implements simple tic-tac-toe game
Python implements the brick-and-mortar game
Python implements the source code of the snake game
Python implements Super Mario
Python implements man-machine gobang
Python, PyGame game project
Python implements image stitching
Python implements scanning tools
Python implements threshold regression
Python implements minesweeper games
Python implements electronic dictionary
Python implements simple tank battle
Python3 realizes airplane war game
Python implements udp chat window
python guess the word game
Python implements parking management system
Python realizes apple eating game
Python implements TCP file transfer
Python realizes the guessing game
Python numpy implements rolling case
OpenCV Python implements puzzle games
Python implements password strength verification
Python implements car management system
Python implements code block folding
Python implements panoramic image stitching
Python implements SMTP mail sending
Python implements multi-dimensional array sorting
How Python implements FTP function
Python implements mean-shift clustering algorithm
Python implements verification code recognition
Python implements gradient descent method
Python implements text version minesweeper
Python implements image stitching function
How Python implements the mail function
Python3 implements the singleton design pattern
Python implements exchange rate conversion operations
Python implements string and number splicing
Python implements ten classic sorting algorithms
Python implements a universal web framework
Python implements 126 mailboxes to send mail
Python implements AI face change function
Python implements the steepest descent method
Python implements the actual banking system
Python implements ftp file transfer function
Python implements username and password verification
How Python implements the timer function
Python implements the aircraft war project
Python implements horizontal stitching of pictures
Python implements GIF graph upside down