我正在做一个猜数字游戏,我有一个“while True”循环,我想一直循环,直到用户猜对了数字。现在我显示了数字以便于测试。不管我猜对与否,我都会收到错误消息“'Nonetype' 对象没有属性'Guess'。” 我很困惑为什么“while True”第一次循环时没有错误,但之后又出现错误。
跟踪器.py
from Number import *
class Runner(object):
def __init__(self, start):
self.start = start
print Integer.__doc__
print Integer.code
def play(self):
next_guess = self.start
while True:
next_guess = next_guess.Guess()
if next_guess == Integer.code:
print "Good!"
exit(0)
else:
print "Try again!"
Integer = Random_Integer()
Game = Runner(Integer)
Game.play()
数字.py
from random import randint
class Random_Integer(object):
"""Welcome to the guessing game! You have unlimited attempts
to guess the 3 random numbers, thats pretty much it."""
def __init__(self):
self.code = "%d%d%d" % (randint(1,9), randint(1,9), randint(1,9))
self.prompt = '> '
def Guess(self):
guess_code = raw_input(self.prompt)
谢谢!