What am I doing wrong? I would like to use count as a global variable.
count = 0
card = low
def running_count ():
if card == low:
count = count 1
elif card == mid:
count = count
elif card == high:
count = count-1
print(count)
I get 0 as the result
CodePudding user response:
count = 0
card = 'low'
def running_count(card):
global count
if card == 'low':
count = count 1
elif card == 'mid':
count = count
elif card == 'high':
count = count-1
running_count(card)
print(count)
#1
running_count(card)
print(count)
#2
But it safer to not use global if you don't have to.
CodePudding user response:
You should try to avoid using global variables where possible.
card should be a string, which you compare to a string inside your function.
You need to call the function you have defined so it gets exectued.
This is an example of how you could do it.
count = 0
card = 'low'
def running_count(count, card):
if card == 'low':
count = count 1
elif card == 'mid':
count = count
elif card == 'high':
count = count-1
return count
count = running_count(count, card)
print(count)
Also you don't really need
elif card == 'mid':
count = count
since you don't change the value of count, you could simply skip this step, because all value besides 'low' and 'high' don't change count.
I hope this helps