0

Reposting with code snippets/better explanation of the problem.

I have a string variable that says "1", "2," 3" etc. Now I have a series of objects named profile_1, profile_2 etc. And each object has a .name attribute. Within a loop, I want to be able to say "whatever this string variable is, that should be the number in the object name, and I want to print the attribute." So when the variable is "1" (string) in the loop, I want to print the value of profile_1.name within a f-string, and the same for 2, and so on.

This is part of a stock market simulation program. Code snippets below to clarify:

This function gets a value for active_broker which will be "1","2" etc.:

def chooseBroker():
        active_broker = input("\nPlease enter the number corresponding to your name: ")

class declaration for Broker class showing name attribute:

class Broker:
    def __init__(self, name, address, bank, acct, balance):
        self.name = name
        self.address = address
        self.bank = bank
        self.acct = acct
        self.balance = balance

example declaration of broker objects named broker_1, etc.:

broker_1 = Broker("Tom Riddle", "Hogwarts", "Gringotts", "456456", 500000)
broker_2 = Broker("Elon Musk", "Space", "Tesla", "012345", 500000)

this code adds a list of stocks to chosen_stocks for each broker, then assigns that list to the broker_chosen_stocks dictionary with the key being "1", "2" for each broker:

broker_chosen_stocks = {}
chosen_stocks = []

def stockList(stock_pick):
    del chosen_stocks[:]
    chosen_stocks.append(stock_pick)

broker_chosen_stocks[f"{active_broker}"] = chosen_stocks

this function prints the components of chosen_stocks for the active broker:

def printCurrentChosenPrice(chosen_stocks):
    print("\nYour chosen investments currently cost:\n")
    for i in range(0, len(chosen_stocks)):
        print(f"{chosen_stocks[i].symbol}: ${chosen_stocks[i].current_price} per share")

This is what I am trying to figure out. For each chosen_stocks list that is an item in the broker_chosen_stocks dictionary, I want to print the broker's name and then their investments. Their name is an attribute of the broker object which is called broker_1. When the value of active_broker variable is "1", I want to print the value of broker_1.name attribute, and the first item in the broker_chosen_stocks dictionary. Then when active_broker is "2" I want the same for broker_2.name and so on.

for key, value in broker_chosen_stocks.items():
     brokername = {broker_(key).name}   (not sure of correct syntax here)
     print(f"\n{brokername}'s investments:")
     printCurrentChosenPrice(value)

If you see what I am trying to do here, can you advise on how to make it work? Thanks in advance.

ng17
  • 1
  • 2
  • 1
    What was the problem with the answers to the question your previous question was closed as a duplicate of? – mkrieger1 Mar 11 '23 at 22:24
  • Instead of `broker_1`, `broker_2`, etc, have a list of dicts called `brokers`, and then you can do an iteration like `for broker in brokers:`. – Samwise Mar 11 '23 at 22:30
  • @mkrieger1 do you mean the "how do I create variable variables" thread? That one advised creating a dictionary or a list. Everything I am referring to in my program is already part of a dictionary/ list. I'm not sure how creating another one would solve my problem. – ng17 Mar 11 '23 at 23:13
  • @Samwise that might actually work... I'd probably have to change my class definition for Broker to include a stock list and then try to call it. I'll try that, thank you! – ng17 Mar 11 '23 at 23:14

0 Answers0