それぞれにオブジェクトを含む複数のリストがあります。すべてのオブジェクトの値は「名前」と「金額」です。私がしたいのは、まずユーザー入力(名前を見つけるために名前を入力)でリスト内の項目を見つけてから、ユーザー入力値を加算または減算することによって「金額」を更新する方法を作成することです(減算する値を追加/ )。リスト内のオブジェクトの値を更新する
どうすればいいですか?
これは私が(それは不完全だが、それは私が成し遂げることができるすべてです)これまで持っているものです。
Containers = []
Lids = []
Wicks = []
Labels = []
Misc = []
class item(object):
#Constructor
def __init__(self, name, amount):
self.name = name
self.amount = amount
#Accessors
def getName(self):
return self.name
def getAmount(self):
return self.amount
#Mutators
def __str__(self):
return "[Name: " + self.name + \
", Amount: " + self.amount + \
"]"
def addAmount():
found = False
name = input("Enter the name of a container you wish to change: ")
addNewAmount = input("Enter the amount you wish to add: ")
for item in Containers:
if name in item.getName():
found = True
position = Containers.index(name)
print(Containers[position])
if not found:
print("No names match that input.")
def subtractAmount():
update = input("Enter a new amount to subtract: ")
self.amount = amount - update
def addContainer():
name = input("Enter a name for the new container: ")
amount = input("Enter an amount for the new container: ")
return item(name, amount)
def addLid():
name = input("Enter a name for the new lid: ")
amount = input("Enter an amount for the new lid: ")
return item(name, amount)
def addWick():
name = input("Enter a name for the new wick: ")
amount = input("Enter an amount for the new wick: ")
return item(name, amount)
def addLabel():
name = input("Enter a name for the new label: ")
amount = input("Enter an amount for the new label: ")
return item(name, amount)
def addMisc():
name = input("Enter a name for the new misc item: ")
amount = input("Enter an amount for the new misc item: ")
return item(name, amount)
def main():
running = True
while running:
print("Enter a number to start.")
print("1) Add new container 2) Add new lid")
print("3) Add new wick 4) Add new label")
print("5) Add new misc Item 6) Print Inventory")
print("7) Add Amount from item 8) Subtract Amount from item")
print("10) quit")
print("11) print list")
choice = input("> ")
if choice == "1":
Containers.append(addContainer())
elif choice == "2":
Lids.append(addLid())
elif choice == "3":
Wicks.append(addWick())
elif choice == "4":
Labels.append(addLabel())
elif choice == "5":
Misc.append(addMisc())
elif choice == "6":
print("<==========Containers==========>")
for i in Containers:
print(i)
print("<=============Lids=============>")
for i in Lids:
print(i)
print("<=============Wicks============>")
for i in Wicks:
print(i)
print("<============Labels============>")
for i in Labels:
print(i)
print("<==========Misc Items==========>")
for i in Misc:
print(i)
elif choice == "7":
return addAmount()
elif choice == "8":
return subtractAmount()
elif choice == "10":
quit()
elif choice == "11":
print('[%s]' % ', '.join(map(str, Containers)))
else:
print("Invalid entry, please try again.")
if __name__ == "__main__":
main()
[OK]を、私はこのコードをしようとしているし、今私はエラーにこだわっている:「はAttributeError: 『タプル』オブジェクトが属性 『金額』を持っていない、なぜこれが起こっているわからないことがあります。 print(new_item.amount)の83行目で起こっています グローバル変数new_itemに変数として量が確実に追加されます そして、下のコードの各選択肢からすべてのprint(new_item.amount)行を削除した場合"TypeError: 'tuple'オブジェクトが呼び出し可能でないことを示すContainers.append(new_item)でエラーが発生する また、addAmount()メソッドはまだ変更されていません。その位置の値。 – cstmxyz
私はそれを働かせました。最後にはそこにいてはならない方法で何かを呼び出すと、それは私にエラーを与えていました。あなたのすべての協力に感謝します!私はファイルを持っていて、リストを外部ファイルにエクスポートして保存し、プログラムをロードするときにそれらをロードすることさえできました! – cstmxyz