如果声明永远不会奏效

时间:2014-03-20 20:46:53

标签: python class oop python-2.7

If statement底部的最后main()似乎永远不是真的。输出应显示"G"。但它没有。

它或者方法decide()永远不会将self.choice更改为"G"

有什么想法?

这是我的代码:

## FF Encounter Mapper - New OOP ##

RNLUT = (*snip* - a massive tuple)

class Field(object):
    """A field segment."""
    def __init__(self, name, danger_inc, distance):
        self.name = name
        self.danger_inc = danger_inc
        self.distance = distance

    def get_seg(self):
    # return current segment
        return self.segment

    def get_stats(self):
    # return field stats
        return self.name, self.danger_inc, self.distance

    def dinc(self):
    # return field danger increment
        return self.danger_inc


class Player(object):
    """A player."""
    def __init__(self, step_id, danger, b_loop):
        self.step_id = step_id
        self.danger = danger
        self.b_loop = b_loop
        self.rnd = (RNLUT[self.step_id] - self.b_loop) & 0xFF
        self.danger_limit = ((self.rnd + 1) * 256)
        self.segment = []
        self.choice = "W"

    def get_stats(self):
    # return player stats
        return self.step_id, self.danger, self.danger_limit, self.b_loop, self.choice

    def step(self, dinc):
    # take one step and update stats
        self.choice = "W"
        self.danger = (self.danger + dinc)
        if self.danger > 65535:
            self.danger = dinc
        self.step_id += 1
        if self.step_id > 255:
            self.step_id = 0
        if self.step_id == 0:
            self.b_loop = self.b_loop + 13
        if self.b_loop > 255:
            self.b_loop = 0     
        self.step_id += 1                               
        if self.step_id > 255:
            self.step_id = 0
        if self.step_id == 0:
            self.b_loop = self.b_loop + 13                  
        if self.b_loop > 255:
            self.b_loop = 0
        self.rnd = (RNLUT[self.step_id] - self.b_loop) & 0xFF
        self.danger_limit = ((self.rnd + 1) * 256)

    def is_fight(self):
    # return true if the current step is a battle
        return self.danger > self.danger_limit

    def decide(self, choice):
    # make a decision: glitch or battle (default: walk)
        if choice == "G":
            self.choice == "G"
        elif choice == "B":
            self.choice == "B"
            self.danger = 0
        print self.choice

    def log(self):
    # add stats to segment log
        self.segment.append(self.get_stats())


def main():
    myfield = Field("test_field", 113, 256)
    cloud = Player(250,0,13)
    dinc = myfield.dinc()
    print "initial: ", cloud.get_stats()
    print "Run the whole segment:"
    while len(cloud.segment) < 128: # walk field distance
        cloud.step(dinc)
        if cloud.is_fight() == True:        # if there's a battle
            cloud.decide("G")       # perform Glitch every battle
        cloud.log()                 # add stats and decision to segment log
    for i in cloud.segment:
        print i                     # output log


main()

输出错误:

...
(86, 5198, 9472, 26, 'W')
(88, 5311, 48640, 26, 'W')
(90, 5424, 4096, 26, 'W')    # THIS SHOULD BE "G" 
(92, 5537, 65280, 26, 'W')   # BECAUSE self.danger > self.danger_limit

...

Entry&#34; 90&#34;应该是&#34; G&#34;如果decide()正常工作......

1 个答案:

答案 0 :(得分:3)

在决定功能中,而不是分配&#39; G&#39; to self.choice:

if choice == "G":
    self.choice = "G"

你写了一个错字:

if choice == "G":
    self.choice == "G"