Random module bug in Python
by IamtheSenate from LinuxQuestions.org on (#5K195)
Hi all,
I'm creating a game called Monsters 'n' Aliens which I'm hoping to use with a class to teach procedural programming. I've hit a bug on my system (Linux Mint 20.1) which means the random module no longer seems to work.
The gameplay is set that the user is presented with either an alien or monster, the choice of which is determined at random and the user can fight (if player wins they gain a life and enemy loses 1 and vice versa if a loss) and or concede (player loses a life).
However since last night, the random variable no longer works and the same enemy stays persistent throughout the 10 rounds of the game and the player/enemy no longer gain or lose lives.
Can anyone see if there is anything I've missed bug wise?
Code:
import os, random
from os import system
from random import randint
#TODO filter out any non "C" or "F" player inputs
#TODO fix bug which means everything is considered a draw
# Constants
GAME_ROUND = 0
PLAYER_LIVES = 5
ENEMY_LIVES = 5
SPRITE = ""
MONSTER = "MONSTER"
ALIEN = "ALIEN"
ROUNDS = 5
# Global Variables
random_chance = randint(0,10)
sprite = SPRITE
monster = MONSTER
alien = ALIEN
game_round = GAME_ROUND
player_lives = PLAYER_LIVES
enemy_lives = ENEMY_LIVES
rounds = ROUNDS
game = True
enter = ""
# ASCII Art
alien_art = """
_..._
.' '.
/ \ / \
( | | )
(`"` " `"`)
\ /
\ ___ /
"""
monster_art = """
oo`'._..---.___..-
(_,-. ,..'`
`'. ;
: :`
_;_;
"""
welcome_msg = """
Welcome to the game, you have 3 lives and will face either a monster or alien in 10 rounds. If you fight and lose
you will lose a life, while if you win the bad guy will lose a life. The first to reach 0 lives automatically loses.
If you survive until round 10 whoever has the most lives wins. Best of luck!
"""
def introduction(welcome_msg,space):
print("MONSTERS AND ALIENS")
print(welcome_msg)
enter = input("Press ENTER to begin")
if enter == "":
os.system('cls' if os.name == 'nt' else 'clear')
else:
enter = input("Press ENTER to begin")
def choose_sprite(random_chance,sprite):
if random_chance >= 5:
sprite = monster
elif random_chance <5:
sprite = alien
print("In this round you will fight the " + sprite)
if sprite == monster:
print(monster_art)
elif sprite == alien:
print(alien_art)
def fight(random_chance, player_lives,enemy_lives,sprite):
player = input("Do you choose to fight (press F) or do you concede (press C)?:")
if player.upper() == "F":
print("You chose to fight, good luck.")
if random_chance >= 5:
print("")
print("You defeated the "+sprite+", you gain 1 life")
print("Current number of lives: ",player_lives)
print("")
player_lives += 1
enemy_lives -= 1
elif random_chance <5:
print("")
print("You were defeated by the "+sprite+", you lose 1 life")
print("Current number of lives: ",player_lives)
print("")
player_lives -= 1
enemy_lives += 1
if player.upper() == "C":
print("")
print("You choose to concede, you lose one life")
print("Lives = ",player_lives)
print("")
player_lives -= 1
def game_end():
print("Your final score is", player_lives)
if player_lives > enemy_lives:
print("You win, congratulations! :-)")
elif enemy_lives > player_lives:
print("The enemy wins, better luck next time :-(")
elif player_lives == enemy_lives:
print("It was a draw, try again next time :-/")
else:
print("Unfortunately there was an error :-(")
def main():
done = False
while not done:
introduction(welcome_msg,enter)
for i in range(0,rounds):
choose_sprite(random_chance,sprite)
fight(random_chance, player_lives,enemy_lives,sprite)
print("Random Debug Value: ",random_chance)
next_round = input("Press ENTER to begin the next round")
if next_round == "":
os.system('cls' if os.name == 'nt' else 'clear')
else:
next_round = input("Press ENTER to begin the next round")
if player_lives <= 0: done = True
if enemy_lives <= 0: done = True
main()
game_end()
I'm creating a game called Monsters 'n' Aliens which I'm hoping to use with a class to teach procedural programming. I've hit a bug on my system (Linux Mint 20.1) which means the random module no longer seems to work.
The gameplay is set that the user is presented with either an alien or monster, the choice of which is determined at random and the user can fight (if player wins they gain a life and enemy loses 1 and vice versa if a loss) and or concede (player loses a life).
However since last night, the random variable no longer works and the same enemy stays persistent throughout the 10 rounds of the game and the player/enemy no longer gain or lose lives.
Can anyone see if there is anything I've missed bug wise?
Code:
import os, random
from os import system
from random import randint
#TODO filter out any non "C" or "F" player inputs
#TODO fix bug which means everything is considered a draw
# Constants
GAME_ROUND = 0
PLAYER_LIVES = 5
ENEMY_LIVES = 5
SPRITE = ""
MONSTER = "MONSTER"
ALIEN = "ALIEN"
ROUNDS = 5
# Global Variables
random_chance = randint(0,10)
sprite = SPRITE
monster = MONSTER
alien = ALIEN
game_round = GAME_ROUND
player_lives = PLAYER_LIVES
enemy_lives = ENEMY_LIVES
rounds = ROUNDS
game = True
enter = ""
# ASCII Art
alien_art = """
_..._
.' '.
/ \ / \
( | | )
(`"` " `"`)
\ /
\ ___ /
"""
monster_art = """
oo`'._..---.___..-
(_,-. ,..'`
`'. ;
: :`
_;_;
"""
welcome_msg = """
Welcome to the game, you have 3 lives and will face either a monster or alien in 10 rounds. If you fight and lose
you will lose a life, while if you win the bad guy will lose a life. The first to reach 0 lives automatically loses.
If you survive until round 10 whoever has the most lives wins. Best of luck!
"""
def introduction(welcome_msg,space):
print("MONSTERS AND ALIENS")
print(welcome_msg)
enter = input("Press ENTER to begin")
if enter == "":
os.system('cls' if os.name == 'nt' else 'clear')
else:
enter = input("Press ENTER to begin")
def choose_sprite(random_chance,sprite):
if random_chance >= 5:
sprite = monster
elif random_chance <5:
sprite = alien
print("In this round you will fight the " + sprite)
if sprite == monster:
print(monster_art)
elif sprite == alien:
print(alien_art)
def fight(random_chance, player_lives,enemy_lives,sprite):
player = input("Do you choose to fight (press F) or do you concede (press C)?:")
if player.upper() == "F":
print("You chose to fight, good luck.")
if random_chance >= 5:
print("")
print("You defeated the "+sprite+", you gain 1 life")
print("Current number of lives: ",player_lives)
print("")
player_lives += 1
enemy_lives -= 1
elif random_chance <5:
print("")
print("You were defeated by the "+sprite+", you lose 1 life")
print("Current number of lives: ",player_lives)
print("")
player_lives -= 1
enemy_lives += 1
if player.upper() == "C":
print("")
print("You choose to concede, you lose one life")
print("Lives = ",player_lives)
print("")
player_lives -= 1
def game_end():
print("Your final score is", player_lives)
if player_lives > enemy_lives:
print("You win, congratulations! :-)")
elif enemy_lives > player_lives:
print("The enemy wins, better luck next time :-(")
elif player_lives == enemy_lives:
print("It was a draw, try again next time :-/")
else:
print("Unfortunately there was an error :-(")
def main():
done = False
while not done:
introduction(welcome_msg,enter)
for i in range(0,rounds):
choose_sprite(random_chance,sprite)
fight(random_chance, player_lives,enemy_lives,sprite)
print("Random Debug Value: ",random_chance)
next_round = input("Press ENTER to begin the next round")
if next_round == "":
os.system('cls' if os.name == 'nt' else 'clear')
else:
next_round = input("Press ENTER to begin the next round")
if player_lives <= 0: done = True
if enemy_lives <= 0: done = True
main()
game_end()