2 Replies - 827 Views - Last Post: 30 August 2011 - 04:26 AM Rate Topic: -----

#1 Vblaster  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 16-January 11

Python Random Hang

Posted 29 August 2011 - 09:49 PM

This game i'm working on hang's, and when killed with ctrl - C, it returns:
  File "Main.py", line 7, in <module>
    Sniper()
  File "/home/aiden/Desktop/Projects/Live/Strike/SDiv.py", line 28, in Sniper
    Shoot()
  File "/home/aiden/Desktop/Projects/Live/Strike/SDiv.py", line 17, in Shoot
    Dam = random()


What is wrong with the random statement that hang's?

from random import *
def Sniper():
    seed()
    Quest = 0.1
    if Quest == 0.1:
        
        print "Scout: Sniper In Trees, He's your's SnipFire"
        Found = uniform(0.1, 0.2)
        print Found
        if Found < 1:
           EnHealth = 2.0
           Health = 2.0
            
           def Shoot():
                EnHealth = 2.0
                Health = 2.0
                Dam = random()
                EnHealth = EnHealth - Dam
                
                
           def RetFire():
                EnHealth = 2.0
                Health = 2.0
                DamS = random()
                Health = Health - DamS
                
           while Health > 0:
                Shoot()
                RetFire()
                
           else:
                print "Game Over"
                
           if EnHealth == 0:
                print "You Killed The Sniper!"
                Kills += 1



Is This A Good Question/Topic? 0
  • +

Replies To: Python Random Hang

#2 Python_4_President  Icon User is offline

  • D.I.C Regular

Reputation: 53
  • View blog
  • Posts: 321
  • Joined: 13-August 11

Re: Python Random Hang

Posted 29 August 2011 - 11:54 PM

Run that. You'll see pretty quickly that Health is always > 0.



from random import uniform
from random import seed
from random import random
def Sniper():
    seed()
    Quest = 0.1
    if Quest == 0.1:
        
        print "Scout: Sniper In Trees, He's your's SnipFire"
        Found = uniform(0.1, 0.2)
        print Found
        if Found < 1:
           EnHealth = 2.0
           Health = 2.0
            
           def Shoot():
                EnHealth = 2.0
                Health = 2.0
                Dam = random()
                EnHealth = EnHealth - Dam
                
                
           def RetFire():
                EnHealth = 2.0
                Health = 2.0
                DamS = random()
                Health = Health - DamS
                
           i = 0
           while Health > 0:
                print "woot! Infinite loop!"
                print Health
                i += 1
                print i
                Shoot()
                RetFire()
                
           else:
                print "Game Over"
                
           if EnHealth == 0:
                print "You Killed The Sniper!"
                Kills += 1


Sniper()



The issue here is with scope. If you consider the below example, you'll see that it won't compile because 'a' is not defined within the scope of its use.

Each declaration within a function creates an independent variable by that name, so the 'a' in t1 is not the same as the 'a' in t2, which isn't the same as any of the other functions, either.

And the scope which actually does something with 'a' doesn't have a definition of 'a'.

Thus, where you're adding to/subtracting from health, you're doing it on independent instances of that variable which have no bearing on the test for whether or not Health is > 0, because this Health is set to 2.0, and nothing exists that says otherwise.



def t1():
    a = 0
    
def t2():
    a = 1

def t3():
    a = 2

def t4():
    a = 3
    
    
    
t1()
print a
t2()
print a
t3()
print a
t4()
print a





Classes are kind of awesome for this particular sort of thing...

Whenever I come upon a problem that requires me to keep tracking of something, like health points, or whatever, I like to use classes.

Really, Enemy and Hero are the same class, just with a couple of overridden methods that I didn't actually override (I explicitly programmed them instead).


Enemy/Hero could be the single class Contestant with derivative classes Hero and Enemy with customized sayings and whatnot. (and weapons!)

from random import uniform
from random import seed
from random import random
import time
class Sniper():
    
    class Enemy():
        def __init__(self):
            self.Health = 2.0
            self.Found = False
            self.Alive = True
            
        def Roll(self):
            if 36 > uniform(1, 10000):
                self.Found = True
            elif 39 < uniform(1, 10000):
                self.Found = False
            elif 2300 < 2*uniform(1, 10000):
                self.Found = False
            elif 983 > uniform(1, 10000)/3:
                self.Found = True
                
        def isFound(self):
            return self.Found
        
        def HasBeenFound(self):
            self.Found = True
        
        def isAlive(self):
            return self.Alive
        
        def HasDied(self):
            if self.Health <= 0:
                self.Alive = False
                print "Enemy has perished!"
                return True
        
        def Damage(self, damage = uniform(0.1, 0.2)):
            print "Enemy has suffered ", damage, " points of damage!"
            self.Health -= damage
        
        def Attack(self):
            damage = random()
            print "Enemy inflicted ", damage, " points of damage!"
            return damage
            
            
    class Hero():
        def __init__(self):
            self.Health = 2.0
            self.Found = False
            self.Alive = True
            
        def Roll(self):
            if 36 > uniform(1, 10000):
                self.Found = True
            elif 39 < uniform(1, 10000):
                self.Found = False
            elif 2300 < 2*uniform(1, 10000):
                self.Found = False
            elif 983 > uniform(1, 10000)/3:
                self.Found = True
                
        def isFound(self):
            return self.Found
        
        def HasBeenFound(self):
            self.Found = True
        
        def isAlive(self):
            return self.Alive
        
        def HasDied(self):
            if self.Health <= 0:
                self.Alive = False
                print "Hero has perished!"
                return True
        
        def Damage(self, damage = uniform(0.1, 0.2)):
            print "Hero has suffered ", damage, " points of damage!"
            self.Health -= damage
        
        def Attack(self):
            damage = random()
            print "Hero inflicted ", damage, " points of damage!"
            return damage
    
    
    
    
    def __init__(self, Quest = 0.1): #__init__ is an initialization routine that gets called whenever you do Sniper()
        seed()                       #I assume Quest is meaningful, so by saying __init__ can have arguments, you can do Sniper(Quest=0.3) or so, default 0.1
        self.Quest = Quest #self here because typing a class name over and over again is lame. Outside of the class definition you have to use Sniper.Quest
        
        
        
    def Engine(self):
        Jim_Rambo = self.Hero()
        CobraCommander = self.Enemy()
        while Jim_Rambo.isAlive() and CobraCommander.isAlive():
            print "Scout: Sniper In Trees, He's your's SnipFire"
            time.sleep(2)
            Jim_Rambo.Roll()
            CobraCommander.Roll()
            if(Jim_Rambo.HasBeenFound()):
                Jim_Rambo.Damage()
            elif(CobraCommander.HasBeenFound()):
                CobraCommander.Damage()
            else:
                CobraCommander.Damage(Jim_Rambo.Attack())
                Jim_Rambo.Damage(CobraCommander.Attack())
                
                if(Jim_Rambo.HasDied()):
                    print "Cobra Winzzzzzzzzzzz!"
                elif(CobraCommander.HasDied()):
                    print "Ayokaakahhooo WIN!"
            
            



game = Sniper()
game.Engine()




Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5874
  • View blog
  • Posts: 12,752
  • Joined: 16-October 07

Re: Python Random Hang

Posted 30 August 2011 - 04:26 AM

from random import *

def Sniper():
	seed()
	Quest = 0.1
	# well, of course it does now, why have this?
	# if Quest == 0.1:
        
	print "Scout: Sniper In Trees, He's your's SnipFire"
	Found = uniform(0.1, 0.2)
	print Found
	if Found < 1:
		EnHealth = 2.0
		Health = 2.0
		# defining funciotn here makes no sense.
		# Particularly when they're almost doing the same think
		# perhaps things would be easier to follow if we just
		# put the logic in the loop
            
		# def Shoot():
		# def RetFire():
                
		while Health > 0:
			# Shoot()
			EnHealth = 2.0 # magically healed
			Health = 2.0 # magically healed
			Dam = random() # 0..0.9999, never gonna die
			EnHealth = EnHealth - Dam
		
			# RetFire()
			EnHealth = 2.0 # magically healed
			Health = 2.0 # magically healed
			DamS = random() # 0..0.9999, never gonna die
			Health = Health - DamS
		# I don't really understand where this else goes to
		# do you?
		# else: 
		print "Game Over"
                
		if EnHealth == 0:
			print "You Killed The Sniper!"
			Kills += 1



How about we use functions in a way that will show you what's going on?

from random import *

# functions allow us to generalize repetative tasks.
def doDamage(attackerName, defenderName, attakerDamage, defenderHealth):
	print defenderName, " has ", defenderHealth, " health."
	print attackerName, " does ", attakerDamage, " damage."
	defenderHealth -= attakerDamage
	print defenderName, " now has ", defenderHealth, " health."
	print
	return defenderHealth

def sniper():
	playerHealth = 2.0
	enemyHealth = 2.0
	isPlayerTurn = True # you have to start somewhere

	# why check both?  Well, we really want to know who died
	while playerHealth > 0 and enemyHealth > 0:
		if isPlayerTurn:
			enemyHealth = doDamage("player", "sniper", random(), enemyHealth)
		else:
			playerHealth = doDamage("sniper", "player", random(), playerHealth)
		isPlayerTurn = not isPlayerTurn # flip flop the turns
	print "Game Over"
		
	if playerHealth > 0:
		print "You Killed The Sniper!"
	else:
		print "The Sniper Killed you!"

sniper()



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1