无法在类中保留持久变量

时间:2016-03-22 04:26:46

标签: python oop

我正在尝试编写一个可以猜测1-100之间的数字的脚本,并根据用户是否说这个数字过高或过低来做出适当的响应,直到猜到正确的数字为止。我的代码现在运行(Python 2.7),但即使我继续调用新的make_guess,猜测也不会更新。我对它应该如何进行的想法是:

  1. 初步猜测为50
  2. 获知是否更高,更低或更正
  3. 根据2中函数调用返回的答案调整猜测并更新my_guess变量
  4. 继续,直到被告知猜测是正确的
  5. 这是我目前的代码:

    class Number_Guesser(object):
        lower_bound = 1
        upper_bound = 100
    
        def initialize(self,lower_bound,upper_bound):
            self.lower_bound = lower_bound
            self.upper_bound = upper_bound
            self.number_of_guesses = 1
    
        def guess_and_adjust(self):
            while True:
                self.anticheat()
                my_guess = self.make_guess()
                answer = input('Enter 1 if your number is higher, 2 if it\'s lower, or 3 if I was correct!')
                if answer=='1':
                    self.lower_bound = my_guess + 1
                    self.number_of_guesses = self.number_of_guesses + 1
                elif answer=='2':
                    self.upper_bound = my_guess - 1
                    self.number_of_guesses = self.number_of_guesses + 1
                elif answer=='3':
                    print 'Woohoooo! I guessed your number in {0} guesses!'.format(self.number_of_guesses)
    
        def anticheat(self):
            if self.lower_bound > self.upper_bound:
                print 'You cheated! We\'re done here!'
                quit()
    
        def make_guess(self):
            self.current_guess = (self.upper_bound + self.lower_bound)/2
            print 'my guess is {0}'.format(self.current_guess)
            return self.current_guess
    
    n = Number_Guesser()
    n.guess_and_adjust()
    

    基本上,我的问题是我的代码出错了,导致我的my_guess变量没有更新?

1 个答案:

答案 0 :(得分:1)

有些观点:

  • 您的代码无效,因为在Python 2中(但不是3!),内置的input()函数是调用eval()(求值,即将字符串转换为Python对象)输入字符串。这意味着,如果您输入1 [ENTER],则不会获得"1",而是1,结果是raw_input()。更改此方法的最佳方法是使用initialize代替,而不会评估输入流。您还应检查无效输入。

  • 永远不会调用__init__方法。这是因为,在Python中,“构造函数”函数称为MyClass(1, 2, 3)。因此,当且仅当MyClass.__init__(self, 1, 2, 3)被定义时,调用MyClass.__init__才会调用lower_bound,而且不在您的情况下。

  • 由于上述原因,在课堂内声明upper_bound__init__并不是一个好主意。将它们作为参数传递给x = x + y而不是。

  • 您可以说x += y,而不是使用quit()模式。

  • 找到合适的答案时,您应该class Number_Guesser(object): def __init__(self,lower_bound,upper_bound): self.lower_bound = lower_bound self.upper_bound = upper_bound self.number_of_guesses = 1 def guess_and_adjust(self): while True: self.anticheat() my_guess = self.make_guess() answer = raw_input('Enter 1 if your number is higher, 2 if it\'s lower, or 3 if I was correct!') if answer=='1': self.lower_bound = my_guess + 1 self.number_of_guesses += 1 elif answer=='2': self.upper_bound = my_guess - 1 self.number_of_guesses += 1 elif answer=='3': print 'Woohoooo! I guessed your number in {0} guesses!'.format(self.number_of_guesses) quit() else: print 'Hey, that input is invalid!' def anticheat(self): if self.lower_bound > self.upper_bound: print 'You cheated! We\'re done here!' quit() def make_guess(self): self.current_guess = (self.upper_bound + self.lower_bound) / 2 print 'My guess is {0}.'.format(self.current_guess) return self.current_guess n = Number_Guesser(1, 100) n.guess_and_adjust() 该程序(或重新启动它)。

考虑到这一点,您的代码应如下所示......

([\w-+]+(?:\.[\w-+]+)*@(?:[\w-]+\.)+[a-zA-Z]{2,7})