LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-13-2021, 02:56 PM   #1
IamtheSenate
LQ Newbie
 
Registered: Feb 2021
Location: Manchester
Distribution: Fedora 38 Xfce and Linux Mint 21.2 Xfce
Posts: 11

Rep: Reputation: Disabled
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()
 
Old 06-13-2021, 03:04 PM   #2
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,601

Rep: Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546
Quote:
Originally Posted by IamtheSenate View Post
random_chance = randint(0,10)
That line occurs once, in the top-level section commented "Global Variables".

 
Old 06-13-2021, 03:21 PM   #3
IamtheSenate
LQ Newbie
 
Registered: Feb 2021
Location: Manchester
Distribution: Fedora 38 Xfce and Linux Mint 21.2 Xfce
Posts: 11

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by boughtonp View Post
That line occurs once, in the top-level section commented "Global Variables".

Thanks, I've moved that line into the specific procedures where the random_chance variable but the same error is still recurring.
 
Old 06-13-2021, 05:06 PM   #4
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,601

Rep: Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546

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.

 
Old 06-13-2021, 05:16 PM   #5
IamtheSenate
LQ Newbie
 
Registered: Feb 2021
Location: Manchester
Distribution: Fedora 38 Xfce and Linux Mint 21.2 Xfce
Posts: 11

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by boughtonp View Post
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.
 
Old 06-14-2021, 02:54 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
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?
 
Old 06-14-2021, 08:36 AM   #7
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082
Quote:
Originally Posted by pan64 View Post
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).
 
Old 06-14-2021, 11:00 AM   #8
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
That is the universal/global/general/perfect solution

Quote:
Originally Posted by ntubski View Post
... moved it to the right place and it started working ...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
I got error while installing python-tk python-psycopg2 python-twisted saili kadam Linux - Newbie 1 09-05-2015 03:03 AM
LXer: Python Python Python (aka Python 3) LXer Syndicated Linux News 0 08-05-2009 08:30 PM
KDE Random wallpaper or script to create symbolic links to random files cvelasquez Linux - Software 2 02-26-2007 06:48 PM
Python guru's - Is this a python bug? or is it me? bardinjw Programming 2 06-23-2005 08:17 AM
Creating random numbers from shell with /dev/random khermans Linux - General 1 07-13-2004 12:12 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:43 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration