Reputation: 969
Just for practice, I'm trying to recreate the game Dudo within Python (I don't think knowledge of the game affects my question that much though). Basically, six players have a certain number of dice (represented by count1, count2 etc.), and I need to know the number of each kind of die (How many 1s, how many 2s etc.). I created a variable num1 (num1 = 0) to represent the number of 1s, and then I made a function to find the number of 1s.
def find_num1():
total = 0
for i in range(you_count):
if you[i] == 1:
total = total + 1
for i in range(count1):
if enemy1[i] == 1:
total = total + 1
for i in range(count2):
if enemy2[i] == 1:
total = total + 1
for i in range(count3):
if enemy3[i] == 1:
total = total + 1
for i in range(count4):
if enemy4[i] == 1:
total = total + 1
for i in range(count5):
if enemy5[i] == 1:
total = total + 1
num1 = total
print total
Each player has a list of six numbers that represents each die they have (enemy1, enemy2 etc.). So I used a loop to go through each list index to find the number 1 and then update a total based on that. When I run the function, it correctly prints total. However, if I tell it to print num1 (outside of the function) it tells me 0, meaning that my function did not correctly change num1 to total. Does anyone have any ideas what I'm doing wrong?
Thanks!
Upvotes: 2
Views: 81
Reputation: 4664
As your programs get longer, having too many global variables will make your program hard to understand, hard to fix when you find bugs, and hard to improve when you want to add features. Fortunately, there's a clean way to pass a value from a function back to the main program: return
. If your top level does foo = some_func()
, and some_func()
ends with return 5
, then 5 will be assigned to foo
after some_func()
finishes. You could rewrite as follows:
def find_num1():
total = 0
for i in range(you_count):
if you[i] == 1:
total = total + 1
for i in range(count1):
if enemy1[i] == 1:
total = total + 1
for i in range(count2):
if enemy2[i] == 1:
total = total + 1
for i in range(count3):
if enemy3[i] == 1:
total = total + 1
for i in range(count4):
if enemy4[i] == 1:
total = total + 1
for i in range(count5):
if enemy5[i] == 1:
total = total + 1
return total
num1 = find_num1()
print(num1)
You can even return multiple values by creating a tuple out of them in the function and unpacking the tuple at top level:
def sum_and_product(a, b):
sum_value = a + b
prod_value = a * b
return (sum_value, prod_value)
(plusle, times) = sum_and_product(3, 5)
# the function returned (8, 15), and after unpacking,
# plusle is now 8 and times is now 15
See also return
examples from Learn Python the Hard Way.
Code in this answer is dual licensed: CC BY-SA 3.0 or the MIT License as published by OSI.
Upvotes: 1
Reputation: 59284
Tell the program num1
is a global variable
def find_num1():
global num1 #Here
total = 0
for i in range(you_count):
if you[i] == 1:
total = total + 1
for i in range(count1):
if enemy1[i] == 1:
total = total + 1
for i in range(count2):
if enemy2[i] == 1:
total = total + 1
for i in range(count3):
if enemy3[i] == 1:
total = total + 1
for i in range(count4):
if enemy4[i] == 1:
total = total + 1
for i in range(count5):
if enemy5[i] == 1:
total = total + 1
num1 = total
print total
Upvotes: 1