class Card(object):
companyName = ""
companyValue = ""
positive = True
checkpower = True
def printcard(self):
if self.positive:
if self.checkpower:
print(f"{self.companyName}")
if not self.checkpower:
print(f"{self.companyName} +{self.companyValue}")
if not self.positive:
print(f"{self.companyName} -{self.companyValue}")
card = Card()
powerCard = Card()
cardStack = []
companyNames = ['Vodafone', 'YesBank', 'Cred', 'TCS', 'Reliance', 'Infosys']
powerCards = ['RightsIssue', 'ShareSuspend', 'LoanStock', 'Debenture', 'Currency']
for num in range(6):
company = companyNames[num]
for every in range(num+1):
for sign in range(2):
if sign == 0:
card.positive = True
if sign == 1:
card.positive = False
value = (every+1) * 5
card.companyName = str(company)
card.companyValue = str(value)
card.checkpower = False
cardStack.append(card)
#card.printcard()
#card.printcard()
for num in cardStack:
num.printcard()
它给我的输出是: 信息系统-30 信息系统-30 信息系统-30 信息系统-30 信息系统-30 ...(乘以卡数)
并尝试在for循环中打印卡,如您所见,该循环已注释。它们生成时完全不会在此处粘贴,因为它会不必要地冗长