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.
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
Außerdem haben Sie nach einigen 'print' Anweisungen falsche Leerzeichen und keine Leerzeichen vor' oder' – MichaelMaggs