简体   繁体   English

我无法在定义函数中返回值

[英]I can't return a value in define function

I've been trying to make a fighting system which works on its own and all I gotta do is input the values, though I get a problem at the end:我一直在尝试制作一个独立运行的战斗系统,我要做的就是输入值,尽管最后我遇到了问题:

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)

I want to make it so at the end it returns hpf, or better yet hp, so after the fight is over, the hp and mp are saved, though I can't get why it's not working.我想让它在最后返回 hpf,或者更好的是 hp,所以在战斗结束后,hp 和 mp 被保存,尽管我不明白为什么它不起作用。

调用fmode函数时需要保存返回值:

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

You are only returning values if enemyhp < = 0 , and even then you are not saving the return values:您只if enemyhp < = 0返回值,即使这样您也不会保存返回值:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM