Python骰子游戏没有按预期工作

时间:2014-04-07 10:24:50

标签: 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))

1 个答案:

答案 0 :(得分:1)

您只需向else添加if/elif即可。就目前而言,&#34;都能幸存下来&#34; line与#34相同的代码块; c2在战斗中死亡&#34;,这并没有太多意义。

此外,我认为你想使用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")