import time
health = 100
kidsHP = 10
def kidsenemyattack():
global health
print('The enemy deals damage!')
health -= 4
print(health)
print('==========')
time.sleep(3)
kidsenemyattack()
def kidfight():
global kidsHP
ready = input('You can attack!')
if ready in ['1','one','One']:
kidsHP -= 1
time.sleep(2.5)
while health > 0:
kidsenemyattack() and kidfight()
if kidsHP <= 0:
print('You've defeated the evil kids")
kidsenemyattack()
只是覆盖kidfight()
,反之亦然。我希望他们两个都能在没有一个凌驾于另一个之上的情况下履行自己的职责。
您正在函数定义中递归执行kidsenemyattack
。从那里删除它,从逻辑上讲,代码就有意义了。在代码运行之前,我也必须解决一些格式问题。下面是一个工作示例:
import time
health = 100
kidsHP = 10
def kidsenemyattack():
global health
print('The enemy deals damage!')
health -= 4
print(health)
print('==========')
time.sleep(3)
# kidsenemyattack() <- this is causing your repeating issue
def kidfight():
global kidsHP
ready = input('You can attack!')
if ready in ['1','one','One']:
kidsHP -= 1
time.sleep(2.5)
while health > 0:
kidsenemyattack()
kidfight()
if kidsHP <= 0:
print("Youve defeated the evil kids")
exit(0)
print("Youre defeated")
运行它产生了一个工作场景:
$ python kidsf.py
The enemy deals damage!
96
==========
You can attack!1
The enemy deals damage!
92
==========
You can attack!3
The enemy deals damage!
88
==========
You can attack!12
The enemy deals damage!
84
==========
You can attack!
# this goes on