Python Forum
Question with while loop placement
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Question with while loop placement
#1
I'm trying to make a game that has a "fight" loop that it runs when the user encounters a demon. I pretty much have a basic version done but was wondering how to format a while loop so that the user keeps attacking until they or the enemy runs out of health. When I run the program I don't get any errors, it successfully randomizes critical attacks, regular attacks, and other stuff. If someone could help me understand how to make a while loop that keeps the attacks going. Any help would be appreciated, thanks!
import random

DemonOne = 0
DemonTwo = 1
DemonThree = 2
#DemonOneStats
DemonOneHealth = 25
DemonOnePower = 25
DemonOneLuck = 250
DemonOneLuckDisplayed = DemonOneLuck/10
DemonOneArmour = 10
DemonOneAttack = DemonOnePower/4
DemonOneAttackCalc = (DemonOneLuck/25) - 1
#DemonTwoStats
DemonTwoHealth = 50
DemonTwoPower = 50
DemonTwoLuck = 100
DemonTwoLuckDisplayed = DemonTwoLuck/2
DemonTwoArmour = 25
#DemonThreeStats
DemonThreeHealth = 100
DemonThreePower = 100
DemonThreeLuck = 50
DemonThreeLuckDisplayed = DemonThreeLuck * 2
DemonThreeArmour = 100
#UserStats
BaseHealth = 100
BaseLuck = 100
BaseLuckDisplayed = BaseLuck/2
BasePower = 100
Armour = 0
BaseAttack = BasePower/4
#FirstAttack
FirstAttackCalc = (BaseLuck/25) - 1
def stats():
    print("Health: " + str(BaseHealth), "Luck: " + str(BaseLuckDisplayed), "Power: " + str(BasePower), "Armour: " + str(Armour), "Weapon(s) equipped: " )
def DemonOneStats():
    print("The demon's stats are: Health- "+str(DemonOneHealth),"Luck- "+str(DemonOneLuckDisplayed), "Power- "+str(DemonOnePower), "Armour- " +str(DemonOneArmour))
def DemonTwoStats():
    print("The demon's stats are: Health- "+str(DemonTwoHealth),"Luck- "+str(DemonTwoLuckDisplayed), "Power- "+str(DemonTwoPower), "Armour- " +str(DemonTwoArmour))
def DemonThreeStats():
    print("The demon's stats are: Health- "+str(DemonThreeHealth),"Luck- "+str(DemonThreeLuckDisplayed), "Power- "+str(DemonThreePower), "Armour- " +str(DemonThreeArmour))

DemonChooser = random.randint(0,2)
FirstAttack = random.randint(0,FirstAttackCalc)
DemonOneCritical = random.randint(0,DemonOneAttackCalc)


#DemonOneFightLoop
if DemonChooser == 0:
    DemonOneStats()
    #while DemonOneHealth <= 0 or BaseHealth <= 0 is True:
    if FirstAttack != 0:
            DemonOneHealth = DemonOneHealth - BaseAttack
            print("You attack the demon first:")
            DemonOneStats()
            if FirstAttack == 2:
                DemonOneHealth = DemonOneHealth - BaseAttack
                print("You caught the enemy off guard! Critical hit!")
                DemonOneStats()
            elif FirstAttack == 0:
                BaseHealth = BaseHealth - DemonOneAttack
                print("You were unlucky and the demon attacked you first:")
                stats()
            if DemonOneCritical == 0:
                BaseHealth = BaseHealth - DemonOneAttack
                print("You were caught off guard by the demon! Critical hit!")
                stats()
elif DemonChooser == 1:
    DemonTwoStats()
elif DemonChooser == 2:
    DemonThreeStats()
Reply
#2
You could make it a bit easier by using a dictionary for demons like:
demons = {
    0: {
        'name': 'DemonOne',
        'health': 25,
        'power': 25,
        'luck': 250,
        'luck_display': 0,
        'armour': 10,
        'attack': 0,
        'attack_calc': 0
    },
    1: {
        'name': 'DemonTwo',
        'health': 25,
        'power': 25,
        'luck': 250,
        'luck_display': 0,
        'armour': 10,
        'attack': 0,
        'attack_calc': 0
    },
    2: {
        'name': 'DemonThree',
        'health': 25,
        'power': 25,
        'luck': 250,
        'luck_display': 0,
        'armour': 10,
        'attack': 0,
        'attack_calc': 0
    }
}

def initialize_demons():
    demons[0]['luck_display'] = demons[0]['luck'] / 10
    demons[0]['attack'] = demons[0]['power'] / 4
    demons[0]['attack_calc'] =  (demons[0]['luck'] / 25) - 1
    demons[1]['luck_display'] = demons[1]['luck'] / 10
    demons[1]['attack'] = demons[1]['power'] / 4
    demons[1]['attack_calc'] =  (demons[1]['luck'] / 25) - 1
    demons[2]['luck_display'] = demons[2]['luck'] / 10
    demons[2]['attack'] = demons[2]['power'] / 4
    demons[2]['attack_calc'] =  (demons[2]['luck'] / 25) - 1

initialize_demons()

# Example usage:
# To get power:
def get_power(demon_no):
    return demons[demon_no]['power']

def get_name(demon_no):
    return demons[demon_no]['name']

def get_attack(demon_no):
    return demons[demon_no]['attack']

def display_some_values(demon_no):
    print('{}: power: {}, attack: {}'.format(get_name(demon_no), get_power(demon_no), get_attack(demon_no)))

# for DemonTwo
display_some_values(1)
When run, results:
Output:
DemonTwo: power: 25, attack: 6.25
You could take this a step further by saving the initial dictionary values as a json file
Reply
#3
Thank you for this advice, I really appreciate it. As someone who is new to Python and these forums, tips, such as the one you gave me, are really helpful and make me a better programmer. In addition to the dictionary, do you have any suggestions on how to format a while loop so that the attacks keep on happening until someone gets 0 health? Any help with this while loop issue would be greatly appreciated. Thanks
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Preventing Duplicate Placement in 2D Array nickdavis2017 2 1,529 Feb-03-2022, 11:06 PM
Last Post: nickdavis2017
  A question about 'Event loop is closed' fc5igm 2 2,165 Oct-05-2021, 02:00 AM
Last Post: fc5igm
Exclamation question about input, while loop, then print jamie_01 5 2,616 Sep-30-2021, 12:46 PM
Last Post: Underscore
  for loop question KEYS 1 1,697 Oct-27-2020, 11:42 PM
Last Post: jefsummers
  Netmiko Loop question sc00ter 2 3,263 Oct-24-2020, 10:54 PM
Last Post: sc00ter
  while loop question KEYS 2 1,976 Sep-26-2020, 11:02 PM
Last Post: KEYS
  New to programming, loop question tomyan 1 1,589 Sep-25-2020, 04:32 PM
Last Post: Larz60+
  while loop question spalisetty06 2 1,813 Aug-13-2020, 04:18 PM
Last Post: buran
  question about for loop Than999 5 2,427 Jun-09-2020, 02:16 PM
Last Post: Emekadavid
  Data placement nboogerz 1 1,811 May-16-2020, 11:20 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020