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'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