Posts: 26
Threads: 10
Joined: Jun 2018
My problem involves the 'intro' variable. Adding stuff works just fine, but if I type in 'remove' for the 'intro' var, it goes to the part where i add stuff, instead of removing stuff.
inv = {"food":
{"apple": "amount",
"orange": "amount",
"watermelon": "amount",
"grape": "amount",
"pear": "amount",
"banana": "amount",
"tomato": "amount"},
"electronics":
{"televison": "amount",
"desktop": "amount",
"laptop": "amount",
"printer": "amount",
"cords": "amount",
"phone": "amount"},
"tools":
{"hammer": "amount",
"drill" : "amount",
"screwdrive": "amount",
"saw": "amount",
"ax": "amount"},
"furniture":
{"couch": "amount",
"chair": "amount",
"table": "amount",
"shelf": "amount"}}
intro = input("What would you like to do?: ")
if intro == "add" or "Add":
where = input("Where would you like to add?: ")
item = input("What would you like to add?: ")
amount = input("How much would you like to add?: ")
inv[where][item] = amount
print(item + ": " + amount)
elif intro == "remove" or "Remove":
where = input("Where would you like to remove?: ")
item = input("What would you like to remove?: ")
amount = input("How much would you like to remove?: ")
inv[where][item] = item - amount
Posts: 4,220
Threads: 97
Joined: Sep 2016
You are using 'or' incorrectly. See this post.
Posts: 26
Threads: 10
Joined: Jun 2018
Aug-21-2018, 03:16 PM
(This post was last modified: Aug-21-2018, 03:17 PM by 2skywalkers.)
Ok, it worked, thank you for the help.
I also came across a other problem. When I try to remove items, it says:
Error: Line 46: TypeError: unsupported operand type(s) for Sub: 'str' and 'str'
For example, I have ten apples, and I want to remove to, so then there are 8 apples, but the error message above comes up.
inv = {"food":
{"apple": 10,
"orange": 0,
"watermelon": 0,
"grape": 0,
"pear": 0,
"banana": 0,
"tomato": 0},
"electronics":
{"televison": 0,
"desktop": 0,
"laptop": 0,
"printer": 0,
"cords": 0,
"phone": 0},
"tools":
{"hammer": 0,
"drill" : 0,
"screwdrive": 0,
"saw": 0,
"ax": 0},
"furniture":
{"couch": 0,
"chair": 0,
"table": 0,
"shelf": 0}}
intro = input("What would you like to do?: ")
if intro == "add" or intro == "Add":
where = input("Where would you like to add?: ")
item = input("What would you like to add?: ")
amount = input("How much would you like to add?: ")
inv[where][item] = amount
print(item + ": " + amount)
elif intro == "remove" or intro == "Remove":
where = input("Where would you like to remove?: ")
item = input("What would you like to remove?: ")
amount = input("How much would you like to remove?: ")
inv[where][item] = item - amount
Posts: 4,220
Threads: 97
Joined: Sep 2016
If you are running this in Python 3.x (which you should be), input returns strings. So you are storing the numbers as strings, not as integers. You need to convert the amounts to integers with the int() built-in:
>>> int('801')
801
>>> int('801') - 5
796
>>>'801' - 5 # causes an error.
Posts: 26
Threads: 10
Joined: Jun 2018
Aug-21-2018, 03:45 PM
(This post was last modified: Aug-21-2018, 03:45 PM by 2skywalkers.)
Ok, i got that part figured out, thanks, but im confused.. again. Apple already has 10, but when I go to add it, if I want to add 2, so it is 12, it sets it equal to 2. I also can't figure out the remove part at all.
intro = input("What would you like to do?: ")
if intro == "add" or intro == "Add":
where = input("Where would you like to add?: ")
item = input("What would you like to add?: ")
amount = int(input("How much would you like to add?: "))
inv[where][item] += amount
print(item + ": " + str(amount))
elif intro == "remove" or intro == "Remove":
where = input("Where would you like to remove?: ")
item = input("What would you like to remove?: ")
amount = int(input("How much would you like to remove?: "))
inv[where][item] = item - int(amount)
print(item + ": " + str(amount))
Posts: 4,220
Threads: 97
Joined: Sep 2016
It's working for me, and I think it's working for you, it's just that your output is misleading. Note that you are printing amount. That's what is added or subtracted, not the end result. The show the end result, you should print(inv[where][item]).
Also, if you want to do a case insensitive match, the typical way to do it is if intro.lower() == 'add': . The lower method converts the string to lowercase, so you just have to check against lowercase. Sorry, I should have mentioned that earlier.
Posts: 26
Threads: 10
Joined: Jun 2018
Aug-23-2018, 04:24 PM
(This post was last modified: Aug-23-2018, 04:24 PM by 2skywalkers.)
I tried to figure out this one on my own for a while, but nothing I did worked. On the 'elif inventory' part I want to print out everything in a organized fashion. Like 'Apples: (amount)' and do that for every item. I would like some help please, if someone could lead me in the right direction I would appreciate it please, thank you.
inv = {"food":
{"apple": 0,
"orange": 0,
"watermelon": 0,
"grape": 0,
"pear": 0,
"banana": 0,
"tomato": 0},
"electronics":
{"televison": 0,
"desktop": 0,
"laptop": 0,
"printer": 0,
"cords": 0,
"phone": 0},
"tools":
{"hammer": 0,
"drill" : 0,
"screwdrive": 0,
"saw": 0,
"ax": 0},
"furniture":
{"couch": 0,
"chair": 0,
"table": 0,
"shelf": 0}}
while True:
intro = input("What would you like to do?: ")
if intro.lower() == "add":
where = input("Where would you like to add?: ")
item = input("What would you like to add?: ")
amount = int(input("How much would you like to add?: "))
inv[where][item] += amount
print(item + ": " + str(inv[where][item]))
elif intro.lower() == "remove":
where = input("Where would you like to remove?: ")
item = input("What would you like to remove?: ")
amount = int(input("How much would you like to remove?: "))
inv[where][item] -= amount
print(item + ": " + str(inv[where][item]))
elif intro.lower() == "inventory":
print("hi")
end = input("Would you like to quit?: ")
if end == "yes":
break
elif end == "no":
continue
Posts: 4,220
Threads: 97
Joined: Sep 2016
Well, you're going to want to loop over your dictionary if you want to print it all. The way I usually loop over a dictionary is with the items() method of the dictionary. That method returns a sequence of pairs of the keys of the dictionary and the values associated with those keys.
for category, items in inv.items():
print(category, items) If you do that, you will see it print tools (or maybe one of the other inv keys) and the dictionary with the different tools and their amounts. But really, you want to print individual tools and their amounts. So you want another loop, within the loop above, to loop over items in a similar way.
|