2016-03-23 20 views
-2

Kodum:Python hatası: "Küresel adı 'sayacı1' tanımlı değil"

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)

emin neden bu hatayı alıyorum değil mi? Bana global değişken counter1'i tanımladım. Ben de bu hatayı karşıdan2 için alıyorum ve hatayı çözmeyi başarabilmemin tek yolu add1 = 0 (veya başka bir sayı) additive() yönteminin döndürme ifadesinin üzerindeki tek bir satırın eklenmesidir. Yardım çok takdir edilecektir! çözümlerin

+0

Lütfen sorunuzdaki tüm geri izlemeyi sağlayın. – zondo

+3

Global yapmak yerine, buna erişmek için 'Persistence.counter1' komutunu kullanın veya bir örnek niteliği oluşturun. – L3viathan

+1

Genel bir değişken yerine bir sınıf niteliği oluşturdunuz. L3viathan'ın yorumuna göre 'Persistence.counter1' veya' self.counter1' işlevini kullanın. Genel olarak, yine de genel değişkenlerden kaçının. – Evert

cevap

0

biri gibi sınıfın dışına sayaçları taşımaktır:

# global variables 
    num = 0 
    counter1 = 0 
    counter2 = 0 

    class Persistence: 
     .... 
     .... 

değiştirilmeden kod kalanını bırakın. sınıf şu anda niteliklerini olarak sadece classname olarak erişmeyi, bu şekilde tutmak istiyorsanız

0

counter1 ve counter2, tanımlanmıştır. attrname:

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): 
     ... 
    def sum_digits(self, num): 
     ... 
    def times_digits(self, num): 
     ... 
    def additive(self, num): 
     sum1 = self.sum_digits(num) 
     list1 = [] 
     list1 = self.digitize(sum1) 
     if list1.__len__() > 1: 
      Persistence.counter1 = Persistence.counter1 + 1 
      self.additive(sum1) 
     return sum1, Persistence.counter1 
    def multiplicative(self, num): 
     prod1 = self.times_digits(num) 
     list1 = [] 
     list1 = self.digitize(prod1) 
     if list1.__len__() > 1: 
      Persistence.counter2 = Persistence.counter2 + 1 
      self.multiplicative(prod1) 
     return prod1, Persistence.counter2 

c = Persistence(5) 
print c.additive(5) 
print c.multiplicative(5) 
0

init yöntem için 3 sınıf değişkenleri taşımak olacak bu sorunu çözmek uygun şekilde. Nesne 'kendini' aslında kullanmıyorsanız eğer yapıcı NUM yok

class Persistence(object): 
    def __init__(self):  
     self.counter1 = 0 
     self.counter2 = 0   

    def digitize(self, num): 
     return [int(digit) for digit in str(num)] 

    def sum_digits(self, num): 
     return sum(self.digitize(num)) 

    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): 
     sum1 = self.sum_digits(num) 
     list1 = [] 
     list1 = self.digitize(sum1) 
     if len(list1) > 1: 
      self.counter1 += 1 
      self.additive(sum1) 
     return sum1, self.counter1 

    def multiplicative(self, num): 
     prod1 = self.times_digits(num) 
     list1 = [] 
     list1 = self.digitize(prod1) 
     if len(list1) > 1: 
      self.counter2 += 1 
      self.multiplicative(prod1) 
     return prod1, self.counter2 

c = Persistence() 
print c.additive(5) 
print c.multiplicative(5) 

kullanarak erişilebilir olan bu değişken birleşmiş hale geleceğine.

İlgili konular