Python 我能';t在define函数中返回一个值

Python 我能';t在define函数中返回一个值,python,python-3.x,Python,Python 3.x,我一直在尝试制作一个独立工作的战斗系统,我所要做的就是输入值,尽管最后我遇到了一个问题: import time import random hp=80 atk1=5 atk2=7 mp=40 smite1=12 smite2=15 def fmode(atkf1,atkf2,enemyatk1,enemyatk2,enemy,enemyhp,xp): hpf=hp mpf=mp while True: if enemyhp>0: yt=0 print("Th

我一直在尝试制作一个独立工作的战斗系统,我所要做的就是输入值,尽管最后我遇到了一个问题:

import time
import random
hp=80
atk1=5
atk2=7
mp=40
smite1=12
smite2=15
def     fmode(atkf1,atkf2,enemyatk1,enemyatk2,enemy,enemyhp,xp):
 hpf=hp
 mpf=mp
 while True:
  if enemyhp>0:
   yt=0
   print("The "+str(enemy)+" attacks!")
   time.sleep(1)
   dmg=random.randint(enemyatk1,enemyatk2)
   hpf=hpf-dmg
   print("The "+str(enemy)+" hit you for "+str(dmg))
   time.sleep(1)
   print("You are at "+str(hpf)+"hp")
   time.sleep(1)
   print("Your turn")
   time.sleep(1)
   yt=1
   print("1)Attack "+" 2)Ability")
   while True:
    if yt==1:
     inpy=input("Select:")
     if inpy=="1":
      dmg=random.randint(atkf1,atkf2)
      print("You hit the "+str(enemy)+" for "+str(dmg)+"hp")
      enemyhp=enemyhp-dmg
      time.sleep(1)
      print("The "+str(enemy)+" is at "+str(enemyhp)+"hp")
      time.sleep(1)
      yt=0
      break
     if inpy=="2":
      print("1)Backstab "+" 2)Heal "+str(mpf)+" mana left")
      while True:
       inpy1=input("Select:")
       if inpy1=="1":
        dmg=random.randint(smite1,smite2)
        enemyhp=enemyhp-dmg
        mpf=mpf-15
        print("You stab the "+str(enemy)+" for "+str(dmg))
        time.sleep(1)
        print("The "+str(enemy)+" is at "+str(enemyhp)+"hp")
        time.sleep(1)
        yt=0
        break
    if yt==0:
     break
  if enemyhp<=0:
   print("The "+str(enemy)+" has been defeated.")
   time.sleep(1)
   print(str(xp)+"XP gained")
   time.sleep(1)
   return hpf,mpf
fmode(5,7,1,3,"rat",20,15)
print(hpf)
print(mpf)
导入时间
随机输入
马力=80
atk1=5
atk2=7
mp=40
smite1=12
smite2=15
def fmode(atkf1、atkf2、enemyatk1、enemyatk2、敌军、enemyhp、xp):
hpf=hp
强积金=mp
尽管如此:
如果enemyhp>0:
yt=0
打印(“敌人+攻击!”)
时间。睡眠(1)
dmg=random.randint(enemyatk1,enemyatk2)
hpf=hpf-dmg
打印(“+str(敌人)+”为“+str(dmg)”击中你)
时间。睡眠(1)
打印(“您在”+str(hpf)+“hp”)
时间。睡眠(1)
打印(“轮到你了”)
时间。睡眠(1)
yt=1
打印(“1)攻击“+”2)能力”)
尽管如此:
如果yt==1:
inpy=输入(“选择:”)
如果inpy==“1”:
dmg=random.randint(atkf1,atkf2)
打印(“你击中“+str(敌人)+”代表“+str(dmg)+”hp”)
enemyhp=enemyhp dmg
时间。睡眠(1)
打印(“+str(敌人)+”位于“+str(敌人)+”hp)
时间。睡眠(1)
yt=0
打破
如果inpy==“2”:
打印(“1)背刺“+”2)治疗“+str(强积金)+”法力左)
尽管如此:
inpy1=输入(“选择:”)
如果inpy1==“1”:
dmg=random.randint(smite1,smite2)
enemyhp=enemyhp dmg
强积金=强积金-15
打印(“你刺伤了”+str(敌人)+“代表”+str(dmg))
时间。睡眠(1)
打印(“+str(敌人)+”位于“+str(敌人)+”hp)
时间。睡眠(1)
yt=0
打破
如果yt==0:
打破

如果enemyhp在调用
fmode
函数时需要保存返回:

hpf, mpf = fmode(5,7,1,3,"rat",20,15)

如果enemyhp<=0
,则仅返回值
,即使如此,也不保存返回值:

hpf, mpf = fmode(5,7,1,3,"rat",20,15)
print(hpf)
print(mpf)

我没有考虑将hpf和mpf分配给fmode…但这显然是可行的。谢谢。@Rain因为变量hpf和mpf是在函数fmode中声明的,所以当您想要打印它们时,您无权访问它们。通过在函数中返回它们,您做了正确的事情-您只需要在当前范围中保存它们,这样打印就可以工作了。这只是另外两个VAR,可以命名为任何名称(即使不是hpf/mpf,它仍然有效)@Rain如果这有助于您找到解决方案,请接受答案!是的,我是这个网站的新手,不习惯做这样的事情。我想我把它作为一个答案投了票。还有,不喜欢是怎么回事?我没有想过把hpf和mpf分配给fmode…但这显然是可行的。谢谢。