首页 > 解决方案 > 代码未返回预期结果

问题描述

我正在尝试制作一款小型文字冒险游戏。

为什么这个语句不返回 90?

orc = raw_input("Does the orc attack or miss?")#Replacing with a random number thing later
health = 100

takeahit = 0

def damage(orc):
  if orc == "hit":
    return takeahit == 10 # Ive tried plus as well im having similar problems with choices in inventory
  elif orc == "miss":
    print "The Orc misses"

healthtwo = health - takeahit

print healthtwo

标签: python

解决方案


以适当的方式从函数返回:

def damage(orc):
    print(orc)
    if orc == "hit":
        takeahit = 10 #MODIFIED
    elif orc == "miss":
        takeahit=0
        print "The Orc misses"
    return takeahit  #ADDED STATEMENT

您需要调用定义的函数。在函数定义后添加

takeahit=damage(orc)

推荐阅读