我正在尝试制作一个函数,该函数将玩家的 hp、max hp 和 ammount 综线并返回新的 hp,但不会返回高于最大 hp 的新 hp。我一定在某个地方做错了数学,但不知道在哪里。我的第一次尝试:
def heal(hp,max_hp,heal):
if hp > heal:
return (max_hp + heal)
else:
overflow = heal-max_hp
new_hp = hp - overflow
return (new_hp)
hp = heal(10,30,20)
print hp #prints 20, should print 30
hp = heal(10,30,10)
print hp #prints 30, should print 20
hp = heal(10,20,30)
print hp #prints 0, should print 20.
我的第二次尝试:
def heal(hp,max_hp,heal):
if hp > heal:
return (max_hp + heal)
else:
overflow = max_hp - heal
new_hp = hp - overflow
return (new_hp)
hp = heal(10,30,20)
print hp #prints 0, should print 30
hp = heal(10,30,10)
print hp #prints -10, should print 20
hp = heal(10,20,30)
print hp #prints 20, should print 20.