为什么我得到的值为零?

2024-10-01 04:48:23 发布

您现在位置:Python中文网/ 问答频道 /正文

import random

import time
def header():
  printNow("The program is starting now, prepare to be slain")
  monsterTest();
def monsterTest():
 for i in range(0,1):
    currentHp = 99;
    printNow("Starting Health is: "+str(currentHp))
    randomHit = random.randint(0,50)
    attack1 = currentHp - randomHit
    printNow("Monster attacks for: "+str(randomHit))
    printNow("Remaining Health is: "+str(currentHp-randomHit))
    battle1 = attack1
    printNow(">>>")
    printNow("Next Attack Incoming!")
    printNow(">>>")
    time.sleep(1.5)
    currentHp = battle1
    printNow("Current Health is: "+str(currentHp))
    attack2 = currentHp - randomHit
    printNow("Monster attacks again for: "+str(attack2))
    battle2 = currentHp - attack2
    printNow("After That we are left with "+str(battle2)+" HP")
 footer();


def footer():
  printNow(">>>")
  printNow("Thank you for using Cidadel's Battle Simulator, this concludes our test...")

printNow(header())

The program is starting now, prepare to be slain
Starting Health is: 99
Monster attacks for: 38
Remaining Health is: 61
>>>
Next Attack Incoming!
>>>
Current Health is: 61
Monster attacks again for: 23
After that we are left with 38 HP
>>>
Thank you for using Citadel's Battle Simulator, this concludes our test...
*None*
>>> 

为什么我得到这个none,我认为这与printNow命令有关,但是我不确定我做错了什么导致了实际的none值..:/

我试着正常运行它,但是当我在列表上注释我的行时,我总是得到None值


Tags: importfortimeisdefrandomheaderhealth
1条回答
网友
1楼 · 发布于 2024-10-01 04:48:23

当您这样做时:

printNow(header())

header()的结果被传递给printNow,后者显然会打印它,因为header()返回None,这是由于每个函数末尾都有隐式的return None

将其替换为对header的调用:

header()

相关问题 更多 >