python global counter_Python错误:“未定义全局名称‘counter1’”

我的代码:

class Persistence:

num = 0

counter1 = 0

counter2 = 0

def __init__(self, num):

self.num = num

#num = input("Enter a non-negative number:: ")

if num < 0:

raise NameError("Negative")

#test else:

#print "ok!"

num_list = []

def digitize(self, num):

num_list = []

n = str(num)

for digit in n:

num_list.append(int(digit))

return num_list

def sum_digits(self, num):

the_list = self.digitize(num)

the_sum = 0

for digit in the_list:

the_sum = the_sum + digit

return the_sum

def times_digits(self, num):

the_list = self.digitize(num)

the_product = 0

for digit in the_list:

the_product = the_product * digit

return the_product

def additive(self, num):

global counter1

sum1 = self.sum_digits(num)

list1 = []

list1 = self.digitize(sum1)

if list1.__len__() > 1:

global counter1

counter1 = counter1 + 1

self.additive(sum1)

return sum1, counter1

def multiplicative(self, num):

global counter2

prod1 = self.times_digits(num)

list1 = []

list1 = self.digitize(prod1)

if list1.__len__() > 1:

global counter1

counter2 = counter2 + 1

self.multiplicative(prod1)

return prod1, counter2

c = Persistence(5)

print c.additive(5)

print c.multiplicative(5)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值