ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: Fedora 38 Xfce and Linux Mint 21.2 Xfce
Posts: 11
Rep:
Random module bug in Python
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()
Ok... if I can be blunt, that response is what I'd expect from someone in a class learning programming.
You can't explain to others something you don't understand yourself, so spend the time going through a few books which teach the fundamentals - there's a relevant one available online here - and lot of other resources linked via the Python Wiki.
Distribution: Fedora 38 Xfce and Linux Mint 21.2 Xfce
Posts: 11
Original Poster
Rep:
Quote:
Originally Posted by boughtonp
Ok... if I can be blunt, that response is what I'd expect from someone in a class learning programming.
You can't explain to others something you don't understand yourself, so spend the time going through a few books which teach the fundamentals - there's a relevant one available online here - and lot of other resources linked via the Python Wiki.
The reason I don’t understand the error is that it was working as expected the night before without any changes being made to the logic. Admittedly I’m relearning programming after quite a while of not using it, rest assured I won’t be teaching programming as the main part of my occupation.
1. It is definitely not a bug in the random module.
2. We have no idea what did you modify (and how does your script look like now) - in post #3.
3. You ought to try debugging and/or logging to understand what's going on and to be able to fix it.
4. What was changed in the last night?
1. It is definitely not a bug in the random module.
2. We have no idea what did you modify (and how does your script look like now) - in post #3.
We do have some idea: clearly wherever the randint(0,10) call was moved to was the wrong place, since the OP reported no effect. I moved it to the right place and it started working (although there are a few additional bugs also due to confusion between global and local variables).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.