help
这是一个你出数字,电脑猜的小程序。。我对他的调用不是很清楚。麻烦高手指点下啊
import random
class ComputerGuessingGame:
"""Class to guess a number randomly"""
def __init__( self, value, lowerBound = 0, upperBound = 10 ):
"""ComputerGuesser constructor; takes secret number, lower
and upper bounds"""
self.realValue = value
# keep value within upper and lower bound
if lowerBound > value:
self.lower = value
else:
self.lower = lowerBound
if upperBound < value:
self.upper = value + 1
else:
self.upper = upperBound
def __iter__( self ):
"""Return iterator for object of class ComputerGuesser"""
return self
def next( self ):
"""Guesses a new value. If correct, raises StopIteration;
otherwise returns guess"""
guess = random.randrange( self.lower, self.upper )
if guess == self.realValue:
raise StopIteration
else:
return guess
def main():
# retrieve an integer from the user
while 1:
try:
secretNumber = int(
raw_input( "Enter number for computer to guess: " ) )
except ValueError:
print "Please enter an integer."
else:
break
print
computerGuesser = ComputerGuessingGame( secretNumber )
numberOfWrongGuesses = 0
# print the incorrect guesses
for wrongGuess in computerGuesser:
numberOfWrongGuesses += 1
print "Computer guessed: %d" % wrongGuess
print "\nGot secret number after %d wrong guesses." % \
numberOfWrongGuesses
if __name__ == "__main__":
main()
当执行main()函数后,提示我输入一个数字,我选了个数字8。。然后实例化,运行类的构造器。给LOWER和UPPER赋值,再然后呢?然后执行类内的函数吗?不是没调用吗?自动执行吗?小弟初次接触PYTHON,请大虾们指点。。拜托。。感谢