Home  >  Article  >  Backend Development  >  How to implement the digital bomb game in python

How to implement the digital bomb game in python

coldplay.xixi
coldplay.xixiforward
2020-07-20 16:53:536418browse

How to implement the digital bomb game in python

Python-Digital Bomb Game, for your reference, the specific content is as follows

Digital Bomb Game Rules:

In one Within the range of numbers, there is a number as a bomb. Whoever guesses the bomb will be punished. For example, the range is 1~99,
bomb is 60, and then a number is guessed to be 30, and 30 is not a bomb, then the person who guesses the number now The range is narrowed to 30~100,
guessed another number 80, 80 is not a bomb, so now the range is narrowed to 30~80, you cannot guess the value on the boundary every time,
until you or the computer If you guess the bomb correctly, you will be punished. The game is over.

Requirements: You first enter a number if it is not a bomb, then let the computer narrow the range and enter a number. If it is not a bomb, you narrow the range again and enter. Come down. Go to the computer again and repeat until you find the bomb

O. First find the core idea

1, generate the bomb
2, and print the bomb range
3 , Guess once by yourself
4. Reduce the bomb range
5. Computer-generated random numbers (computer guess once)
6. Continue to reduce the bomb range
7. Loop these operations until the bomb explodes and the game ends !

Code

import random
x =random.randint(1,100)
print("炸弹数字XX")
start = 1#定义最小范围
end = 100#定义最大范围
while True:
 num = int(input("输入{}-{}之间的整数:".format(start, end)))
 if num>x:
 end = num
 print("你猜大了")
 elif num == x:
 print("你输了,游戏结束!")
 break
 elif num < x:
 start = num
 print("你猜小了")
#----------------------------computer(小红)
 computer = random.randint(start,end)
 print("小红认为炸弹是:",computer)
 if computer>x:
 end = computer
 print("小红猜大了")
 elif computer == x:
 print("你赢了")
 print("游戏结束")
 break
 elif computer<x:
 start=computer
 print("小红猜小了")

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

Use random to generate random numbers (representing bombs) in (1-100)
Use two variables to define the upper and lower limits of the bomb
Use input to accept the number you guessed, and use if to determine if the number you guessed is greater than the bomb, assign the number you guessed to the upper limit of the bomb end (change the upper limit of the bomb), if it is equal to the bomb ,game 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 and let Xiaohong guess once. Also use if to judge. The upper limit of the change of the large and bomb value is less than the lower limit of the reduction. Until the game is over!

================== Running results==================

Related learning recommendations: python video tutorial

The above is the detailed content of How to implement the digital bomb game in python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:jb51.net. If there is any infringement, please contact admin@php.cn delete

Related articles

See more