程序在将代码段分配到子程序之前工作
Program working before assigning section of code into a subprogram
这是我在将代码分配到子程序后遇到的错误
"NameError: name 'P1D1' is not defined"
这是代码:
def dice_rolling():
#stage 2 dice rolling
#P1D1 means player ones dice one value and so on with P1D2
import random
#player ones turn
print("player ones turn")
P1D1 = random.randint (1,6)
print ("your number is ",P1D1)
P1D2 = random.randint (1,6)
print ("your second number is",P1D2)
#player twos turn
print ("player twos turn")
P2D1 = random.randint (1,6)
print ("your number is" , P2D1)
P2D2 = random .randint (1,6)
print ("your second number is",P2D2)
def score_calculation():
round_number = 0
round_number = round_number + 1
#player 1 score calculation
total_P1 = P1D1 + P1D2
P1_score = P1_score + total_P1
if total_P1 % 2 == 0:
P1_score = P1_score + 10
else:
P1_score = P1_score + 5
if P1D1 == P1D2 :
P1D3 = random.randint (1,6)
P1_score = P1_score + P1D3
#player 2 score calculation
total_P2 = P2D1 + P2D2
P2_score = P2_score + total_P2
if total_P2 % 2 == 0:
P2_score = P2_score + 10
else:
P2_score = P2_score + 5
if P2D1 == P2D2 :
P2D3 = random.randint (1,6)
P2_score = P2_score + P2D3
print("player ones score at the end of round ",round_number, "is" , P1_score)
print(P2_score)
dice_rolling()
score_calculation()
您在函数 dice_rolling()
中创建变量,然后尝试在 score_calculation()
函数中使用它们。
您需要定义全局变量或在 score_calculation()
函数中定义变量,或者您可以将此变量作为参数传递给 score_calculation()
函数
import random
def dice_rolling():
# P1D1 means player ones dice one value and so on with P1D2
import random
# player ones turn
print("player ones turn")
P1D1 = random.randint(1, 6)
print("your number is ", P1D1)
P1D2 = random.randint(1, 6)
print("your second number is", P1D2)
# player twos turn
print("player twos turn")
P2D1 = random.randint(1, 6)
print("your number is", P2D1)
P2D2 = random.randint(1, 6)
print("your second number is", P2D2)
score_calculation(P1D1, P1D2, P2D1, P2D2)
def score_calculation(P1D1, P1D2, P2D1, P2D2):
round_number = 0
round_number = round_number + 1
# player 1 score calculation
total_P1 = P1D1 + P1D2
P1_score = total_P1
if total_P1 % 2 == 0:
P1_score = P1_score + 10
else:
P1_score = P1_score + 5
if P1D1 == P1D2:
P1D3 = random.randint(1, 6)
P1_score = P1_score + P1D3
# player 2 score calculation
total_P2 = P2D1 + P2D2
P2_score = total_P2
if total_P2 % 2 == 0:
P2_score = P2_score + 10
else:
P2_score = P2_score + 5
if P2D1 == P2D2:
P2D3 = random.randint(1, 6)
P2_score = P2_score + P2D3
print("player ones score at the end of round ", round_number, "is", P1_score)
print(P2_score)
dice_rolling()
这是我在将代码分配到子程序后遇到的错误
"NameError: name 'P1D1' is not defined"
这是代码:
def dice_rolling():
#stage 2 dice rolling
#P1D1 means player ones dice one value and so on with P1D2
import random
#player ones turn
print("player ones turn")
P1D1 = random.randint (1,6)
print ("your number is ",P1D1)
P1D2 = random.randint (1,6)
print ("your second number is",P1D2)
#player twos turn
print ("player twos turn")
P2D1 = random.randint (1,6)
print ("your number is" , P2D1)
P2D2 = random .randint (1,6)
print ("your second number is",P2D2)
def score_calculation():
round_number = 0
round_number = round_number + 1
#player 1 score calculation
total_P1 = P1D1 + P1D2
P1_score = P1_score + total_P1
if total_P1 % 2 == 0:
P1_score = P1_score + 10
else:
P1_score = P1_score + 5
if P1D1 == P1D2 :
P1D3 = random.randint (1,6)
P1_score = P1_score + P1D3
#player 2 score calculation
total_P2 = P2D1 + P2D2
P2_score = P2_score + total_P2
if total_P2 % 2 == 0:
P2_score = P2_score + 10
else:
P2_score = P2_score + 5
if P2D1 == P2D2 :
P2D3 = random.randint (1,6)
P2_score = P2_score + P2D3
print("player ones score at the end of round ",round_number, "is" , P1_score)
print(P2_score)
dice_rolling()
score_calculation()
您在函数 dice_rolling()
中创建变量,然后尝试在 score_calculation()
函数中使用它们。
您需要定义全局变量或在 score_calculation()
函数中定义变量,或者您可以将此变量作为参数传递给 score_calculation()
函数
import random
def dice_rolling():
# P1D1 means player ones dice one value and so on with P1D2
import random
# player ones turn
print("player ones turn")
P1D1 = random.randint(1, 6)
print("your number is ", P1D1)
P1D2 = random.randint(1, 6)
print("your second number is", P1D2)
# player twos turn
print("player twos turn")
P2D1 = random.randint(1, 6)
print("your number is", P2D1)
P2D2 = random.randint(1, 6)
print("your second number is", P2D2)
score_calculation(P1D1, P1D2, P2D1, P2D2)
def score_calculation(P1D1, P1D2, P2D1, P2D2):
round_number = 0
round_number = round_number + 1
# player 1 score calculation
total_P1 = P1D1 + P1D2
P1_score = total_P1
if total_P1 % 2 == 0:
P1_score = P1_score + 10
else:
P1_score = P1_score + 5
if P1D1 == P1D2:
P1D3 = random.randint(1, 6)
P1_score = P1_score + P1D3
# player 2 score calculation
total_P2 = P2D1 + P2D2
P2_score = total_P2
if total_P2 % 2 == 0:
P2_score = P2_score + 10
else:
P2_score = P2_score + 5
if P2D1 == P2D2:
P2D3 = random.randint(1, 6)
P2_score = P2_score + P2D3
print("player ones score at the end of round ", round_number, "is", P1_score)
print(P2_score)
dice_rolling()