Question with while loop placement - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Question with while loop placement (/thread-10165.html) |
Question with while loop placement - Tunechi - May-15-2018 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() RE: Question with while loop placement - Larz60+ - May-16-2018 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: You could take this a step further by saving the initial dictionary values as a json file
RE: Question with while loop placement - Tunechi - May-16-2018 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 |