简体   繁体   中英

Problem with while loop in game (python text game)

I have been working on a game and what I have is what I have placed down below. The problem with it is that Whenever you run away from the enemy whether you are successful or not you DO run away and I dont know how to stop this from happening. Does anyone have any suggestions?

edit: in order to solve the problem I deleted the if slime_rank1_chances > 0: because it's supposed to be 100% at the start of the game and I changed the while slime_rank1_health != 0: into while slime_rank1_health > 0:

import random

#rng
dice = random.randrange(1,7)
critical_hit_chance = 15
run_chance = 10

#characters
name_1_health = 10
name_1_attack = 10
name_1_luck = 10
name_1_exp = 0

#enemies
slime_rank1_health = 10
slime_rank1_attack = 1
slime_rank1_luck = 1
slime_rank1_exp = 5
slime_rank1_chances = random.randrange(1,10)

#simulating an attack by a slime rank 1
if slime_rank1_chances > 0:
    print("you are suddenly attacked by a slime rank 1!")
    while slime_rank1_health != 0:
        if random.randrange(1, 16) == critical_hit_chance:
            name_1_health = name_1_health - (slime_rank1_attack*2)
            print("Its a critical hit!")
            print("you have lost " + str(slime_rank1_attack*2) + " health")
        else:
            name_1_health = name_1_health - slime_rank1_attack
            print("you have lost " + str(slime_rank1_attack) + " health")
        print("your current health is " + str(name_1_health))
        slime_rank1_choice1 = input("What will you do?: A)Attack B)Run [A/B]")
        while True:
            if slime_rank1_choice1 in ["A","B"]:
                break
        if slime_rank1_choice1 == "A":
            print("You attack slime rank 1")
            if random.randrange(1, 16) == critical_hit_chance:
                slime_rank1_health = slime_rank1_health - (name_1_attack*2)
                print("You attack and deal "+str(name_1_attack*2)+" damage")
                print("Its a critical hit!")
            else: 
                slime_rank1_health = slime_rank1_health - name_1_attack
                print("You attack and deal "+str(name_1_attack)+" damage") 
        elif slime_rank1_choice1 == "B":
            print("you have chosen to run")
            if random.randrange(1,11) == run_chance:
                print("you have succesfully escaped!")
                slime_rank1_health = slime_rank1_health - slime_rank1_health
            else: 
                print("you failed to escape")
                if random.randrange(1, 16) == critical_hit_chance:
                    name_1_health = name_1_health - (slime_rank1_attack*2)
                    print("Its a critical hit!")
                    print("you have lost " + str(slime_rank1_attack*2) + " health")
                else:
                    name_1_health = name_1_health - slime_rank1_attack
                    print("you have lost " + str(slime_rank1_attack) + " health")
        break

I think your issue is that the code ends every time you try to run, whether or not you were successful. Your if/else statement looks OK, but either way it hits the break at the end.

Consider making a function to manage your game play and smaller functions to manage mechanics like running, attacking, changing hit points, etc. That might help you organize your code better and make things clearer. Keep at it--it's looking good!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM