This is gonna be the last version of this game. Let’s get straight to the point.
- Streak counting: when you win more than 1 times in a row, the robot will tell you;
- Deleted smallest range function.
import random
bomb = random.randint(1, 100)
streak = 0
min_val = 1
max_val = 100
print(‘—————-Number Bomb Game—————-‘)
while True:
try:
player_enter = int(input(‘Pls enter a number in ‘ + str(min_val) + ‘ ~ ‘ + str(max_val) + ‘: ‘))
if player_enter max_val:
print(‘That number is illegal. Pls enter another one.’)
continue
elif player_enter == bomb:
print(‘You have guessed the bomb. Game over. You lose.’)
streak = 0
restart = input(‘Enter “yes” to play again or enter anything else to exit: ‘)
if restart.lower() != ‘yes’:
break
print(‘—————-Number Bomb Game Restarting—————-‘)
bomb = random.randint(1, 100)
min_val = 1
max_val = 100
else:
if player_enter > bomb:
max_val = player_enter – 1
else:
min_val = player_enter + 1
print(‘The range is now ‘ + str(min_val) + ‘ ~ ‘ + str(max_val) + ‘.’)
robot_enter = random.randint(min_val, max_val)
print(‘The robot have guessed ‘ + str(robot_enter) + ‘.’)
if robot_enter == bomb:
print(‘The robot have guessed the bomb. Game over. You won.’)
streak = streak + 1
if streak > 1:
print(‘You have won ‘ + str(streak) + ‘ times in a row! Can you win again?’)
restart = input(‘Enter “yes” to play again or enter anything else to exit: ‘)
if restart.lower() != ‘yes’:
break
print(‘—————-Number Bomb Game Restarting—————-‘)
bomb = random.randint(1, 100)
min_val = 1
max_val = 100
continue
if robot_enter > bomb:
max_val = robot_enter – 1
else:
min_val = robot_enter + 1
except ValueError:
print(“That’s not an integer. Pls enter an integer.”)