I’m having issues calling variables I defined as global within a function. I am able to print the value of the variable but it is when I try to put them into an if statement that they suddenly become “undefined”.
p1_score and p2_score have been defined in functions as global. I have then used the values of the an iterated them with results from the game without issue. However it is when I try to call them outside the function into the if statement below that I have an issue.
def p1_roll():
#my code in the function
p1_score += total_roll #a variable from another function (this works fine)
def p2_roll():
#my code in the function
p2_score += total_roll
The players in the game play for 5 rounds then this is the if condition to determine who wins:
if p1_score == p2_score:
print(“You are both tied. You now get one more roll each.”)
final_roll(number_of_sides)
elif p1_score > p2_score:
print(p1_name.upper +", YOU ARE THE WINNER WITH %s POINTS!" % str(p1_score))
elif p2_roll > p1_score:
print(p2_name.upper +", YOU ARE THE WINNER WITH %s POINTS!" % str(p2_score))