Uri.K Uri.K - 1 month ago 7
Python Question

I need help,calling a function and improving my program any help is much abliged

I'm a beginner to stack overflow aswell as python , if I need to write more code I will just want this to work . I have been stuck for two days not being able to do anything, if their is improvement please tell me so I can fix my code and make it efficient. I also don't know how to call the functions below I listed a box with what I want to do but don't know how to.

import random
import time

startingP_health = 30
startingE_health = 30

def player_attack():
global startingE_health
time.sleep(1)
print ("What ability would you like to use? (free speach(fs), capatalism(c), or punch(p)")
ability_choice = input()

if(ability_choice == "fs"):
enemy_health = startingE_health-3
enemy_heath = int(enemy_health)
elif(ability_choice == "c"):
enemy_health = startingE_health-(random.randint(1,6))
enemy_heath = int(enemy_health)
elif(ability_choice == "p"):
enemy_health = startingE_health-(random.randint(2,4))
enemy_heath = int(enemy_health)
else:
print("you fell.")

time.sleep(1)

print ("Enemie's health is now: ",enemy_health)
print("")

return int(enemy_health)


def enemy_attack():
global startingP_health
time.sleep(1)
print ("Enemy kicks you")
print("")
player_health = startingP_health - (random.randint(1,3)
player_health = int(player_health)
time.sleep(1)
print ("Your health is now ",player_health)
print ("")
return int(player_health)

def battle_trotsky():
global player_health
print ("Enemy appears")
print ("")
time.sleep(1)
while player_health > 0 and enemy_health > 0:
##############################
#call function player_attack
#call enemy_attack
##############################
if player_health <=0:
break
if enemy_health <= 0:
time.sleep(1)
print ("You have killed the enemy")

if player_health <= 0:
print("Sorry you failed the mission you must restart the mission"))

################################
#initate function sequence
################################

Answer

This is a not a good question for this site as it is not specific, but I feel compelled to fixed this code by a deep power. I'm not sure why you're doing the sleeps, the print("") statements can be replaced with a '\n' in the previous prints, and you are continuously reassigning player health to the starting health - number, which will never reach 0, so you must make the healths global.

Here's a working example

import random

player_health = 30
enemy_health = 30

def player_attack():
    global enemy_health
    ability_choice = input("What ability would you like to use? (free speach(fs), capatalism(c), or punch(p)")

    if(ability_choice == "fs"):
        enemy_health -= 3
    elif(ability_choice == "c"):
        enemy_health -= random.randint(1,6)
    elif(ability_choice == "p"):
        enemy_health -= random.randint(2,4)
    else:
        print("you fell.")
    print ("Enemy's health is now: "+str(enemy_health)+'\n')

def enemy_attack():
    global player_health
    print ("Enemy kicks you")
    player_health -= random.randint(1,3)
    print ("Your health is now "+str(player_health)+"\n")

def battle_trotsky():
    print ("Enemy appears\n")
    while player_health > 0 and enemy_health > 0:
        player_attack()
        if check_game_over():
            return
        enemy_attack()
        if check_game_over():
            return

def check_game_over():
    global player_health, enemy_health
    if enemy_health <= 0:
        print ("You have killed the enemy\n")
        return True
    elif player_health <= 0:
        print("Sorry you failed the mission you must restart the mission")
        return True
    else:
        return False

if __name__=='__main__':
    battle_trotsky()
Comments