Pomocy def i wartość zmiennej python

0

Witam próbowałem zrobić walke pokemonów tekstowo ale jak chce wrócić do początku akcji to wartość która ustawiłem Hp po użyciu potki wraca do ustawienia z rozpoczęcia a gdybym ją ustawił przed def pokazuje że nie jest zdefiniowana oto kod:

import time
A = "Attack"
B = "Bag"

print("As you walk through the grass you hear something")
time.sleep(3)
print("Argh! The wild pokemon ecountered")
time.sleep(2)
print("The wild magnemite has appeard")
time.sleep(2)
print("You send charmander!")
time.sleep(1)
def Cha():
    chaHp = 1
Cha()
def Poczatek(ChaHp):
    PotionIlO = 1
    CharMAXHP = 25
    CharSPAT = 41
    CharDEF = 20
    CharSPDEF = 19
    CharSPEED = 23
    CharLVL = 7
    CharEXP = 0
    Scratch = "fizyczny", 20
    Ember = "Specjalny", 35
    MagHp = 18
    MagMAXHP = 18
    MagSPAT = 35
    MagDEf = 32
    MagSPDEF = 27
    MagSPEED = 17
    MagLVL = 5
    ThunderShock = "Special", 25
    if CharEXP >= 10:
        CharLVL + 1
    print("Magnemite LVL:", MagLVL)
    print(MagHp, "/", MagMAXHP)
    print("")
    print("")
    print("Charmander LVL:", CharLVL)
    print(ChaHp,"/",CharMAXHP)
    print("")
    print("")
    time.sleep(1)
    print("what action will you chose?")
    print(A, B)
    Akcja = input(str)
    if Akcja == "Bag":
        print("")
        print("potion X ",PotionIlO)
        print("Will you use potion?")
        print("Yes/No")
        PotionODP = input(str)
        if PotionODP == "Yes":
            if PotionIlO >= 1:
                ChaHp = ChaHp + 20
                PotionIlO - 1
                print("Charmander is healed")
                print("")
                print("Charmander HP:", ChaHp, "/", CharMAXHP)
                if ChaHp >= CharMAXHP:
                    ChaHp = CharMAXHP
            if PotionIlO <= 0:
                PotionIlO = 0
                print("")
                print("You dont have any potions left")
                print("")
                Poczatek()

    if Akcja == "Attack":
        print("")
        print("")
        print("Ember"), print("Scratch")
Poczatek()

jak źle zrobiłem posta to przepraszam jestem tu pierwszy raz

1

Przypuszczam że chcesz wewnątrz funkcji modyfikować zmienną globalną, w takim przypadku pomoże ci global, robi się to tak:

hp = 5


def dodaj():
    global hp
    hp += 1

dodaj()
print(hp)

edit: aczkolwiek lepiej było by zrobić klasę "pokemon" i tam trzymać takie rzeczy jak jego hp i pozostałe atrybuty.

0

Jak sens ma ta funkcja, nie wystarczy chaHp = 1 ?
def Cha():
___ chaHp = 1
Cha()

0

Poza tym przerobiłbym aby Enter był domyślnym Yes:
q = print("Will you use potion [Yes]?")
q = q or "Yes"
if q=="Yes":

0
print ("Will you use potion [Yes]?")
ans = input().lower() #zamienia litery w odpowiedzi na małe jak są jakieś duże
if len(ans) == 0:
    ans = "yes"
if ans == "y" or ans == "yes":
    print("throwing")
else:
    print("rip")

1 użytkowników online, w tym zalogowanych: 0, gości: 1