Function prints the quotation marks and the curly brackets in this for loop function in python
Function prints the quotation marks and the curly brackets in this for loop function in python
The function see_inventory() prints the quotation marks in the for loop inside it, how do i make it so the function prints the list without the quotation marks? I am a begginer btw.
class Person:
equipped_armor =
"head": "name": "cap", "defense": 1,
"chest": "name": "mail", "defense": 2,
"legs": "name": "wooden leggings", "defense": 1,
gold = 10
stored_armor = "Stored Armor: ": ["rash leggings"]
weapons = "Weapons: ": ["wooden_sword", "mythrill_staff"]
potions = "Potions: ": ["HP potion"]
ui_gold = "Gold: ": gold
inv = [
stored_armor,
weapons,
potions,
ui_gold
]
def __init__(self):
self.name = "Cavalex"
def see_inventory(self):
for element in self.inv:
print(element)
By running this:
player = Person()
player.see_inventory()
I get this:
'Stored Armor: ': ['rash leggings']
'Weapons: ': ['wooden sword', 'mythril staff']
'Potions: ': ['HP potion']
'Gold: ': 10
Stored Armor: "rash leggings" Weapons: "wooden sword", "mythrill staff" Potions: "HP Potion" Gold: 10
– Cavalex
Sep 13 '18 at 23:15
Please specify the output you want, and show your coding attempt to produce it. There are many tutorials on output formatting; you will need to access the individual elements you're trying to print.
– Prune
Sep 13 '18 at 23:15
Also, make the code executable.
– bohrax
Sep 13 '18 at 23:18
idownvotedbecau.se/noattempt
– bohrax
Sep 14 '18 at 16:55
3 Answers
3
First, let's reduce your example; the class structure is immaterial to the problem.
gold = 10
stored_armor = "Stored Armor: ": ["rash leggings"]
weapons = "Weapons: ": ["wooden_sword", "mythrill_staff"]
potions = "Potions: ": ["HP potion"]
ui_gold = "Gold: ": gold
inv = [
stored_armor,
weapons,
potions,
ui_gold
]
Now, you're trying to print the list of items without the punctuation that comes naturally with the data storage you chose. To do this, you have to reach inside that storage and reformat the items the way you want to see them.
In this case, it's not qwuite trivial, since you've chosen a non-homogeneous structure for your player's backpack: a list of dictionaries whose values are of differing types. I solved this by cycling through the list (after correcting your mistyped variable name) and checking the type of the value before constructing the output.
for element in inv:
for key, val in element.items():
if type(val) == list:
inventory = ' '.join([item for item in val])
else:
inventory = str(val)
print(key, inventory)
Output:
Stored Armor: rash leggings
Weapons: wooden_sword mythrill_staff
Potions: HP potion
Gold: 10
Thx! Is there a way for me to put "wooden_sword" inside the inventory, but give it values and make it show in the console like "Wooden Sword"? How can i put a comma between the items too?
– Cavalex
Sep 14 '18 at 9:00
@Cavalex: You're now getting into a level of hand-holding that is outside the range of Stack Overflow, and better served by learning string processing and output formatting from tutorials. Yes, you can do these things ... but as the posting guidelines point out, it's better for both you and SO if you search on your own before asking here.
– Prune
Sep 14 '18 at 16:41
Here is a real simple way to do it.
str(player.see_inventory()).replace("'", "")
str(player.see_inventory()).replace("'", "")
Or in the function something like:
print(str(element).replace("'", ""))
print(str(element).replace("'", ""))
The
see_inventory
function doesn't return a value. Using replace
on the str
-repr is somewhat clever, but removes any quotes in the values as well.– bohrax
Sep 13 '18 at 23:42
see_inventory
replace
str
Here you go, just make this small change to see_inventory
this will get the job done
see_inventory
def see_inventory(self):
for element in self.inv:
for k, v in element.items():
if type(v) == list:
print(k, ' '.join(v))
else:
print(k, v)
Output
(xenial)vash@localhost:~/python$ python3.7 armor.py
Stored Armor: rash leggings
Weapons: wooden_sword mythrill_staff
Potions: HP potion
Gold: 10
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
How exactly do you want the output to be?
– bohrax
Sep 13 '18 at 23:13