python - Changing global variable from within a function? -


just practice, i'm trying recreate game dudo within python (i don't think knowledge of game affects question though). basically, 6 players have number of dice (represented count1, count2 etc.), , need know number of each kind of die (how many 1s, how many 2s etc.). created variable num1 (num1 = 0) represent number of 1s, , made function find number of 1s.

def find_num1():     total = 0     in range(you_count):         if you[i] == 1:             total = total + 1     in range(count1):         if enemy1[i] == 1:             total = total + 1     in range(count2):         if enemy2[i] == 1:             total = total + 1     in range(count3):         if enemy3[i] == 1:             total = total + 1     in range(count4):         if enemy4[i] == 1:             total = total + 1     in range(count5):         if enemy5[i] == 1:             total = total + 1     num1 = total     print total 

each player has list of 6 numbers represents each die have (enemy1, enemy2 etc.). used loop go through each list index find number 1 , update total based on that. when run function, correctly prints total. however, if tell print num1 (outside of function) tells me 0, meaning function did not correctly change num1 total. have ideas i'm doing wrong?

thanks!

tell program num1 global variable

def find_num1():     global num1 #here     total = 0     in range(you_count):         if you[i] == 1:             total = total + 1     in range(count1):         if enemy1[i] == 1:             total = total + 1     in range(count2):         if enemy2[i] == 1:             total = total + 1     in range(count3):         if enemy3[i] == 1:             total = total + 1     in range(count4):         if enemy4[i] == 1:             total = total + 1     in range(count5):         if enemy5[i] == 1:             total = total + 1     num1 = total     print total 

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -