简单功能不实习

2024-06-01 00:09:56 发布

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

我正在写一个简单的基于文本的游戏,并且已经写了一个简单的战斗系统。这个系统就像剪刀一样工作。它将迭代循环,甚至不会移动通过第一个打印行。谢谢

这是Python3号

我试过把它变成一个整数,字符串,if to elif和back,还有很多其他的东西。谢谢

def combat(ehealth,ename):
    while (ehealth > 0):
        playerhit=int(input("Would you like to [1] Stab, [2] Swipe, or [3] Bash?   "))
        comhit=random.uniform(1,3)
        if playerhit==1:
            if comhit==1:
                print("The", ename, "parryed the attack")
            elif comhit==2:
                print("The", ename, "blocked the attack")
            elif comhit==3:
                ehealth=ehealth-1
                print("You hit the", ename,"!")
        elif playerhit==2:
            if comhit==1:
                ehealth=ehealth-1
                print("You hit the", ename,"!")
            elif comhit==2:
                print ("The", ename, "parryed the attack")
            elif comhit==3:
                print("The", ename, "blocked the attack")
        elif playerhit==3:
            if comhit==1:
                print("The", ename, "blocked the attack")
            elif comhit==2:
                ehealth=ehealth-1
                print("You hit the", ename,"!")
            elif comhit==3:
                print("The", ename, "blocked the attack")

我希望函数在“ehealth”为零时结束循环

它不会使它传递第一个打印的语句,因为它会一次又一次地循环输入

再次感谢, 史蒂文


Tags: thetoyouif系统printelifattack
1条回答
网友
1楼 · 发布于 2024-06-01 00:09:56
random.uniform(1,3)

这就是问题所在。它返回一个随机浮点数,而不是int。您永远不会到达任何嵌套的if语句

试试random.choice(range(1,4))

或者更好

random.randint(1, 3)

相关问题 更多 >