In a nutshell:
At the end of my program, there is a need to compare two integers whose results are inside the function itself. When I execute, I get undefined variable error.
Actually:
I am creating a hand cricket python script which we usually play as a duo. At last both the scores of the opponents are compared and the greatest wins. The operation with the variables are inside of a function, but when called outside the function, undefined variable error shows up. Help please?
import random
while True:
pc_b_or_b = 0 #PC probability
#User Bowling module
def Bowl():
bat_PC = 0
User_bowl = 0
scoreofpc = 0
ScorePC = 0
while True:
bat_PC = random.randrange(1,11) #Random int gen
User_bowl = int(input("Your turn to bowl: ")) #int from user
if User_bowl<1 or User_bowl>10: #Fool proofing user must not keep less than 1 or gr8 than 10
print("Wrong number")
continue
if User_bowl == bat_PC: # Check if user == pc and out
print()
print("PC Out!")
print("PC Score:",scoreofpc)
break
else: #Continuation
print("Escape for PC. PC kept",bat_PC)
scoreofpc += bat_PC
print("Score:",scoreofpc)
ScorePC = scoreofpc
#User batting module
def User_Batting():
a = 0
score = 0
ManScore = 0
while True:
b = random.randrange(1,11) #Same as above but User is batting and pc randome int gen
a = int(input("Your turn to bat: ")) #But here if user int == pc then out for user
if a<1 or a>10:
print("Wrong number")
continue
if a==b:
print()
print("Out")
print("Score:",score)
break
else:
print("Escape! PC =",b)
score +=a
print("Score:",score)
ManScore = score
Actually Some more code comes here I've reduced to just these as said by StackOverflow
Main problem here, variable not defined, all other modules working perfectly
ScorePC1 = ScorePC
ManScore2 = ManScore
if ScorePC1 > ManScore2:
print("PC won the match by scoring",Score_of_PC)
elif ScorePC1 < ManScore2:
print("You won the match by scoring",User_Score)
else:
print("Your Score and PC score matched!")
quitter = input("Do you wanna Quit? Y/N? ")
if quitter == "yes" or quitter == "y" or quitter == "Y":
print("Thank You for playing!")
print("Script created by ***Techno-Sachin***")
quit()
elif quitter == "n" or quitter == "no" or quitter == "N":
print("Playing again..")
continue
else:
print("It's a wrong input. Try again")
Expectaion:
At last it is expected to print the statements inside if ScorePC1 and ManScore2 comparisons.
Error:
The output is Big, but cut out to focus on the problem itself>
PC Out! PC Score: 38 Traceback (most recent call last): File "C:\Users\E.sachin\Documents\HTML5\Python Scripts\Hand_cricket20.py", line 164, in ScorePC1 = ScorePC NameError: name 'ScorePC' is not defined