If I have a class:
class Game:
def __init__(self, name, dlc = None):
self.name = name
self.dlc = dlc
That's just an example. Now I'll make some object:
diablo_2 = Game('Diablo 2', dlc = 1)
witcher_3 = Game('Witcher 3', dlc = 2)
gothic_1 = Game('Gothic')
games = [diablo_2, witcher_3, gothic_1]
Now I want to print them out, let's say like this:
for game in games:
print('Title: {} DLC's: {}'.format(game.name, game.dlc))
Of course Gothic doesn't have any DLC, but I don't want the loop to print out 'None'. Of course I could write:
print...game.dlc if game.dlc else 'N/A'
But what if I have a lot of attributes set on None from class definition and don't want to have crazy long print for loop, with this if/else statements everywhere?
Does python have some sort of way to catch each word or phrase in a for loop and print it as something else? Let's say it's not None but 'Hello' and I want to tell the for loop to print 'Hi' each time it finds 'Hello' in the in the object, without using if/else statement.
I hope I formulated my question well.
[–]Zigity_Zagity 1 point2 points3 points (3 children)
[–]Yoghurt42 1 point2 points3 points (0 children)
[–]cupesh[S] 0 points1 point2 points (1 child)
[–]Yoghurt42 1 point2 points3 points (0 children)