PYTHON不会打印到.java文档吗?

2024-10-04 03:26:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我对python有意见。 这是我的密码。你知道吗

http://pastebin.com/yRu5WGKd

每当我选择项目或鹤嘴锄,它打印得很好。下面的任何东西都不会打印。。请帮忙!?!你知道吗

注意:我还使用了pastebin,因为我觉得它最容易阅读。你知道吗


Tags: 项目comhttp密码意见pastebinyru5wgkd鹤嘴锄
1条回答
网友
1楼 · 发布于 2024-10-04 03:26:21

你的实际错误是第62行if ItemType == 3 & ItemStr == 1:-它应该以elif开头,否则它会打断你的(真的讨厌)。你知道吗

另一个潜在的问题是:在所有比较中,即if ItemType == 1 & ItemStr == 1:,当您应该使用逻辑and(and)时,您使用的是位and(&)。你知道吗

这是一个重写的版本。它不到长度的一半,由数据驱动,更容易发现不一致的地方(你是说材料类型中的“钻石”还是“祖母绿”地址:

class Item(object):
    types = [
        ('Item',    'Item'),
        ('Pickaxe', 'ItemPickaxe'),
        ('Shovel',  'ItemSpade'),
        ('Axe',     'ItemAxe'),
        ('Hoe',     'ItemHoe'),
        ('Sword',   'ItemSword')
    ]
    strengths = [
        ('Diamond', 'EnumToolMaterial.EMERALD'),    # ?? You might want to doublecheck this...
        ('Gold',    'EnumToolMaterial.GOLD'),
        ('Iron',    'EnumToolMaterial.IRON'),
        ('Stone',   'EnumToolMaterial.STONE'),
        ('Wood',    'EnumToolMaterial.WOOD'),
    ]

    javastring = 'public static final {type} {name} = new {type}({id}, {strength}).setItemName("{name}");'

    @classmethod
    def prompt_for_item(cls):
        s = "Please enter your item's name:\n"
        name = raw_input(s).strip()

        types = ["[{}] {}".format(i,n[0]) for i,n in enumerate(cls.types, 1)]
        s = "Please enter item type:\n{}\n".format('\n'.join(types))
        type_ = int(raw_input(s)) - 1

        s = "Please enter item id (unique int):\n"
        id = int(raw_input(s))

        strengths = ["[{}] {}".format(i,n[0]) for i,n in enumerate(cls.strengths, 1)]
        s = "Please enter item strength:\n{}\n".format('\n'.join(strengths))
        strength = int(raw_input(s)) - 1

        return cls(name, type_, id, strength)

    def __init__(self, name, type_, id, strength):
        self.name = name
        self.type = type_
        self.id = id
        self.strength = strength

    def write_to_file(self, fname=None):
        if fname is None:
            fname = '{}.java'.format(self.name)
        with open(fname, 'w') as outf:
            cls = type(self)
            outf.write(
                cls.javastring.format(
                    type = cls.types[self.type][1],
                    name = self.name,
                    id = self.id,
                    strength = cls.strengths[self.strength][1]
                )
            )

def main():
    it = Item.prompt_for_item()
    it.write_to_file()
    print 'File has been written'

if __name__=="__main__":
    main()

相关问题 更多 >