Python骰子游戏未按预期运行

2024-04-25 01:28:18 发布

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

我需要这个代码的帮助!它本质上允许用户创建两个字符并输入技能值和力量值。它通过从最大值中减去最小值来为每个值创建一个修饰符。为每个角色掷两个骰子,最高的骰子将修改值加到他的值上,最低的骰子将被减去。如果强度小于或等于零,角色就死了。但它不起作用,即使强度小于0,它们也能存活。你知道吗

任何帮助都将不胜感激

import random
c1 = str(input("First Character Name: "))
c1st = int(input("Strength: "))
c1sk = int(input("Skill: "))
c2 = str(input("\nSecond Character Name: "))
c2st = int(input("Strength: "))
c2sk = int(input("Skill: "))
strengthmod = max(c1st, c2st) - min(c1st, c2st)
skillmod = max(c1sk, c2sk) - min(c1sk, c2sk)
c1roll = random.randint(1,6)
c2roll = random.randint(1,6)
print('\n'+ c1 +' rolled a ' + str(c1roll))
print(c2 +' rolled a ' + str(c2roll))

if c1roll > c2roll:
    c1st = c1st + strengthmod
    c1sk = c1sk + skillmod
    c2st = c2st - strengthmod
    c2sk = c2sk - skillmod
elif c2roll > c1roll:
    c2st = c2st + strengthmod
    c2sk = c2sk + skillmod
    c1st = c1st - strengthmod
    c1sk = c1sk - skillmod

if c1st <= 0:
    if c2st <= 0:
        print(c1 + c2 +" have both died in battle")
        c1st == 0
        c2st == 0
    else:
        print(c1 + " died in combat")
        c1st == 0
elif c2st <= 0:
   print(c2 + " died in combat")
c2st == 0
   print("\nBoth had the strength to survive the fight\n")

print(c1+'s Strength is at '+str(c1st) +" and skill is " + str(c1sk))
print(c2+'s Strength is at '+str(c2st) +" and skill is " + str(c2sk))

Tags: inputstrengthintprintc2c1strskillmod
1条回答
网友
1楼 · 发布于 2024-04-25 01:28:18

您只需将else添加到if/elif。从目前的情况来看,“both survive”行与“c2死于战斗”在同一个代码块中,这并没有多大意义。你知道吗

另外,我认为您应该使用c2st = 0而不是c2st == 0,也就是说,如果强度低于零,就将其设置为零。你知道吗

if c1st <= 0:
    ...
elif c2st <= 0:
    print(c2 + " died in combat")
    c2st = 0
else:
    print("\nBoth had the strength to survive the fight\n")

相关问题 更多 >