无论我做什么,当我将修饰符添加到角色强度时它一直说它没有定义如何修复它?

时间:2014-08-04 13:53:04

标签: python

import time
import random

c1_strength = 12
c1_skill = 22
c2_strength = 16
c2_skill = 12

Modifier_skill = int(c1_skill) - int(c2_skill)
print("The skill modifier equals:\n")
print (Modifier_skill)
time.sleep(0.5)

Modifier_strength = int(c2_strength) - int(c1_strength)
print("The strength modifier equals:\n")
print (Modifier_strength)
time.sleep(0.5)

Player_1 = (random.randint(1,6))
print("Player_1 has rolled a 6 sided dice and rolled a:\n")
print(Player_1)

Player_2 = (random.randint(1,6))
print("Player_2 has rolled a 6 sided dice and rolled a:\n")
print(Player_2)

while(Player_1) == (Player_2):
    print("no changes will be made")
    break

if (Player_1) > (Player_2):
    p1_strength = int(c1_strength) + int(Modifier_strength)
print("Player_1's new strength is:\n")
print(p1_strength)

p1_skill = int(c1_skill) + int(Modifier_skill)
print("Player_1's new skill is:\n")
print(p1_skill)

p2_strength = int(c2_strength) - int(Modifier_strength)
print("Player_2's new strength is:\n")
print(p2_strength)

p2_skill = int(c2_skill) - int(Modifier_skill)
print("Player_2's new skill is:\n")
print(p2_skill)

if (Player_1) > (Player_2):
    pl2_strength = int(c2_strength) + int(Modifier_strength)
print("Player_1's new strength is:\n")
print(pl2_strength)

pl2_skill = int(c2_skill) + int(Modifier_skill)
print("Player_1's new skill is:\n")
print(pl2_skill)

pl1_strength = int(c1_strength) - int(Modifier_strength)
print("Player_2's new strength is:\n")
print(pl1_strength)

pl1_skill = int(c1_skill) - int(Modifier_skill)
print("Player_2's new skill is:\n")
print(pl1_skill)

这是我的代码,不知道什么是错的但是在运行时它一直说p1强度没有定义。另一个奇怪的事情是,有时代码会工作一次或两次,但接下来的部分会产生错误,然后以某种方式修复第一部分再次破坏

1 个答案:

答案 0 :(得分:0)

首先,我几乎不确定甚至会发生什么。这是可怕的蟒蛇。问题是p1_strength是在if块

中定义的
if(Player_1 > Player_2):
    p1_strength = int(c1_strength) + int(Modifier_strength)

如果此块未执行,即如果Player_1< = Player_2,p1_strength将未定义。

这可以通过在脚本顶部初始化变量来修复,例如0

c1_strength = 12
c1_skill = 22
c2_strength = 16
c2_skill = 12
p1_strength = 0
pl2_strength = 0