This is a python 3.14 code. It is a game. You have avoid guessing a specific random number between 1 and 100 (the bomb). The range that you can guess will become smaller and smaller as the game continues. There is a robot guessing at the same time. You have to let the robot guess the bomb. Have fun :^)
P.S. The last row “os.system(‘pause’)” is for the program to not close after the loop was broke. This is very useful since the .py program will close automaticly while the program is finished. Using this code, you have plenty of time to look at the output of your program since it pauses. If you don’t have these issues, you could delete this row of code and the second row “import os”. They’re useless for you.
import random
import os
answer = random.randint(1, 100)
min = 1
max = 100
while True:
enter = int(input(‘Pls enter your number between ‘ + str(min) + ‘ and ‘ + str(max) + ‘:’))
if enter > max or enter < min:
print(‘That number is illegal. Pls enter another one.’)
elif enter == answer:
print(‘You have guess the bomb. Game over. You lose.’)
break
else:
if enter > answer:
max = enter – 1
else:
min = enter + 1
robot_enter = random.randint(min, max)
print(‘The robot had guessed’, robot_enter, ‘.’)
if robot_enter == answer:
print(‘The robot have guess the bomb. Game over. You won.’)
break
else:
if robot_enter > answer:
max = robot_enter – 1
else:
min = robot_enter + 1
os.system(‘pause’)