2016-10-13 3 views
-1

Ich versuche, eine Funktion für einen grundlegenden Python-Kurs zu schreiben, die ich nehme. Wir sind an dem Punkt, wo wir uns als Gruppe zusammenschließen und ein Programm als Team machen. Ich habe jedem Mitglied zugeteilt, seinen Teil als eine Funktion in der Hoffnung zu schreiben, dass ich einfach jede Funktion aufrufen kann, um das Gesamtprogramm auszuführen. Es ist eine Weile her, seit ich in der Objektprogrammierung gespielt habe, und das ist über den Klassenanforderungen, aber ich möchte versuchen, dies zum Funktionieren zu bringen.Python eine Variable mit einer Funktion ändern

Ich habe Schwierigkeiten, eine Variable in eine Funktion zu übergeben und dann die geänderte Variable aus der Funktion abzurufen.

Ich habe versucht, auf mehreren Websites und tiefe Suche hier zu lesen, aber ich vermisse etwas und jede Hilfe würde sehr geschätzt werden. Hier

ist die erste Probe von Code habe ich versucht,

import random 
MOBhp=100 

def ATTACK(TYPEatck,MOBhp): 
# global MOBhp 

    print ('the type attack chosen was ',TYPEatck,'the MOB has ',MOBhp) 
    if TYPEatck =='M'or TYPEatck =='m': 
     print ('the ',PLAYER,' used melee to attack',MOB) 
     MOBhp=MOBhp-10 
    elif TYPEatck =='R'or TYPEatck =='r': 
     print ('the ',PLAYER,' used range to attack',MOB) 
     MOBhp=MOBhp-5 
    else: 
     print ('please choose a valid attack') 
    print ('the MOB hitpoints are ',MOBhp) 
    return MOBhp; 


PLAYER='HERO' 
MOB='Dragon' 
AC=12 
while MOBhp > 0: 
    TYPEatck=random.choice('RM') 
    ATTACK(TYPEatck,MOBhp) 
print('really the MOB hitpoints are ', MOBhp)   
print(MOB,'was slain by ',PLAYER) 

Daraus ergibt sich ein sich wiederholendes Ergebnis, dass ich mit CNTRL brechen + c

the type attack chosen was R the MOB has 100 
the HERO used range to attack Dragon 
the MOB hitpoints are 95 
the type attack chosen was M the MOB has 100 
the HERO used melee to attack Dragon 
the MOB hitpoints are 90 
the type attack chosen was R the MOB has 100 
the HERO used range to attack Dragon 
the MOB hitpoints are 95 

Wo, wie, wenn ich die folgende

enter code here 
#while MOBhp > 0: 
TYPEatck=random.choice('RM') 
ATTACK(TYPEatck,MOBhp) 
print('really the MOB hitpoints are ', MOBhp)   
print(MOB,'was slain by ',PLAYER) 

Ich bekomme folgende Ergebnisse

the type attack chosen was R the MOB has 100 
the HERO used range to attack Dragon 
the MOB hitpoints are 95 
really the MOB hitpoints are 100 
Dragon was slain by HERO 

Ich habe versucht, auch mit globalen Variablen zu spielen und kann auch nicht funktionieren.

+0

Bitte korrigieren Sie Ihre Einbuchtung. Python-Code ist sonst nutzlos. Als Vermutung scheinen Sie den Rückgabewert von 'ATTACK' nicht an' MOBhp' zurück zuweisen. – MisterMiyagi

+0

Außerdem haben Sie nach einigen 'print' Anweisungen falsche Leerzeichen und keine Leerzeichen vor' oder' – MichaelMaggs

Antwort

2

Wie es jetzt ist, werfen Sie das Ergebnis Ihrer Berechnung weg. Sie müssen es speichern und als Eingabe für die nächste Berechnung verwenden.

while MOBhp > 0: 
    TYPEatck=random.choice('RM') 
    MOBhp = ATTACK(TYPEatck,MOBhp) 
0

MisterMiyagis Antwort ist hilfreich. Wenn Sie globale Variablen verwenden möchten, können Sie es wie folgt verwenden:

import random 
MOBhp=100 

def ATTACK(TYPEatck, hp): 
    global MOBhp 
    MOBhp = hp 

    print('the type attack chosen was ',TYPEatck,'the MOB has ',MOBhp) 
    if TYPEatck == 'M' or TYPEatck == 'm': 
     print('the ',PLAYER,' used melee to attack',MOB) 
     MOBhp = MOBhp - 10 
     elif TYPEatck == 'R' or TYPEatck == 'r': 
     print('the ',PLAYER,' used range to attack',MOB) 
     MOBhp = MOBhp - 5 
    else: 
     print('please choose a valid attack') 
    print('the MOB hitpoints are ',MOBhp) 
    return MOBhp; 


PLAYER = 'HERO' 
MOB = 'Dragon' 
AC = 12 
while MOBhp > 0: 
    TYPEatck = random.choice('RM') 
    ATTACK(TYPEatck,MOBhp) 

print('really the MOB hitpoints are ', MOBhp)   
print(MOB,'was slain by ',PLAYER) 

Alternativ können Sie Klasse verwenden:

import random 

class Game(): 
    def __init__(self, player, mob): 
     self.player = player 
     self.mob = mob 
     self.mobhp = 100 

    def ATTACK(self, TYPEatck): 
     print('the type attack chosen was ',TYPEatck,'the MOB has ',self.mobhp) 
     if TYPEatck == 'M' or TYPEatck == 'm': 
      print('the ',self.player,' used melee to attack',self.mob) 
      self.mobhp -= 10 
     elif TYPEatck == 'R' or TYPEatck == 'r': 
      print('the ',self.player,' used range to attack',self.mob) 
      self.mobhp -= 5 
     else: 
      print('please choose a valid attack') 
     print('the MOB hitpoints are ',self.mobhp) 
     return self.mobhp; 

    def get_MOBhp(self): 
     return self.mobhp 

PLAYER = 'HERO' 
MOB = 'Dragon' 
AC = 12 
game = Game(PLAYER, MOB) 
while game.get_MOBhp() > 0: 
    TYPEatck = random.choice('RM') 
    game.ATTACK(TYPEatck) 

print('really the MOB hitpoints are ', game.get_MOBhp())   
print(MOB,'was slain by ',PLAYER) 
Verwandte Themen