-2
我有一個python的問題。 這是我的代碼。PYTHON - 不會打印到.java文檔?
每當我選擇項目或鎬,它打印就好了。下面的任何內容都不會打印..請幫助!?!
注意:我還使用了pastebin,因爲我覺得它最容易閱讀。
我有一個python的問題。 這是我的代碼。PYTHON - 不會打印到.java文檔?
每當我選擇項目或鎬,它打印就好了。下面的任何內容都不會打印..請幫助!?!
注意:我還使用了pastebin,因爲我覺得它最容易閱讀。
你實際的錯誤是第62行,if ItemType == 3 & ItemStr == 1:
- 它應該elif
啓動,或者它傷了你的(真正討厭)如果級聯。
另一個潛在的問題:在你所有的比較,即if ItemType == 1 & ItemStr == 1:
,您使用按位與(&
)時,你應該使用邏輯與(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()
你能發佈一個最小的,可編譯的例子?準確的預期輸出是什麼?你會得到什麼?你有什麼嘗試? – Ivan 2012-07-13 17:09:14
善意提供downvotes的原因,以便OP知道爲什麼。 – Levon 2012-07-13 17:09:22
我會推薦學習如何使用Python [字典](http://docs.python.org/tutorial/datastructures.html#dictionaries)。他們會讓你的生活變得更容易。 – Amber 2012-07-13 17:12:20