[英]Creating Text Based RPG Inventory System (python)
我只是想征詢我的意見,盡管我在Python中創建的基於文本的RPG框架非常薄。
class Item(object):
def __init__(self, item):
if(key in Loot):
self.item = Inventory[key]
else:
self.item = None
print('No loot obtain for that decision')
def get_item(self):
return self.item
def item(self):
return str(self.item)
class Inventory(object):
def __init__(self):
self.inventory = []
def add_item(self, item):
self.inventory.append(item)
return self.inventory
def added_loot(self):
print('Congrats, you have obtained a/an' + str(self.item))
當我運行add_loot方法時,當前代碼將清單字典顯示為值。 我想看看是否有什么辦法可以重新制作此代碼,並將字典中的鍵用作我的self.inventory並對其進行隨機排序? 因此,當我打印出str(self.item)時,它將打印出實際的鍵而不是字典中的值。
您可能需要考慮改寫此短語,或者改為查看“代碼審查” 。 您的問題似乎並未要求解決特定問題,尤其是第一行說您只想對代碼發表意見。 堆棧溢出並不是真正的解決之道。
為了解決最后似乎是主要問題的問題,您定義了Item()類,因此您可以考慮添加__str__
或__repr__
方法。 然后,您可以創建一個實例,並通過簡單的print()
調用來print()
先前在所述方法中設置的任何字符串。
test = Item()
print(test)
當然,這將需要修復縮進不正確且沒有屬性的__init__
方法 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.