python - How to return a value from a function and reference it in a while loop -


basically want return value healthcheck function can stop pokemons attacking eachother. see have left out rest of "if" line, unsure put here. need way return function healthcheck function, can't return statement work.

class enemy():     global enemylives     global dead     dead=0     enemylives=10     def healthcheck():         if enemylives<=0:         print("you defeated enemy!")   while true:                time.sleep(1)                enemy1.attacked()                enemy1.healthcheck()                if                     break;                time.sleep(1)                squirtle.attacked()                squirtle.healthcheck() 

(this isn't whole code)

not sure logic of game should create attributes , avoid use of global, use instances attributes , methods. can exit loop sys.exit if enemys life gets 0.

import time import sys random import random  class enemy():     def __init__(self):         self.dead = 0         self.life = 100      def attack(self):         if random() > .20:             self.life -= 10             print("attack successful, enemy  life decreased 10  {}".format(self.life))         elif self.life <= 90:             self.life += 10             print("attack unsuccessful, enemy increases life {}".format(self.life))      def health_check(self):         if self.life <= 0:             print("you defeated enemy!")             sys.exit()   enemy1 = enemy() while true:     time.sleep(1)     enemy1.attack()     enemy1.health_check()     time.sleep(1) 

example output:

attack successful, enemy  life decreased 10  90 attack unsuccessful, enemy increases life 100 attack successful, enemy  life decreased 10  90 attack successful, enemy  life decreased 10  80 attack successful, enemy  life decreased 10  70 attack successful, enemy  life decreased 10  60 attack successful, enemy  life decreased 10  50 attack unsuccessful, enemy increases life 60 attack unsuccessful, enemy increases life 70 attack successful, enemy  life decreased 10  60 attack successful, enemy  life decreased 10  50 attack successful, enemy  life decreased 10  40 attack successful, enemy  life decreased 10  30 attack successful, enemy  life decreased 10  20 attack unsuccessful, enemy increases life 30 attack successful, enemy  life decreased 10  20 attack successful, enemy  life decreased 10  10 attack successful, enemy  life decreased 10  0 defeated enemy! 

you make difficulty attribute set chance of successful attacks increasing or decreasing compare random() to:

class enemy():     def __init__(self,diff):         self.dead = 0         self.life = 100         self.diff = diff      def attack(self):         if random() > self.diff:             self.life -= 10             print("attack successful, enemy  life decreased 10  {}".format(self.life))         elif self.life <= 90:             self.life += 10             print("attack unsuccessful, enemy increases life {}".format(self.life))      def health_check(self):         if self.life <= 0:             print("you defeated enemy!")             sys.exit()   enemy1 = enemy(.20)  

if wanted break in while, return in method , check if:

class enemy():     def __init__(self,diff):         self.dead = 0         self.life = 100         self.diff = diff      def attack(self):         if random() > self.diff:             self.life -= 10             print("attack successful, enemy  life decreased 10  {}".format(self.life))         elif self.life <= 90:             self.life += 10             print("attack unsuccessful, enemy increases life {}".format(self.life))       def health_check(self):         if self.life <= 0:             print("you defeated enemy!")             return true         return false  enemy1 = enemy(.3) while true:         time.sleep(1)         enemy1.attack()         if enemy1.health_check():              break         time.sleep(1) 

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -