-1

I am trying to get text via the return function to concat into a multiline string such as shown below.

btc_positions=[{
'symbol: BTC', 
'margin_balance: 0.15',
'margin_position: 0.05',
'margin_frozen: 0.01'}]

for pos in btc_positions:
    for attributes in pos:
        text = f"{attributes}: {pos[attributes]}"
    return text

text is in this format (symbol: BTC) but throws the error 'return' outside function.

Console Error: python shitcoin_alerts.py File "shitcoin_alerts.py", line 34 return text ^ SyntaxError: 'return' outside function. Fixed! (Return has to be used inside a function)

My intention is to return text to create the multiline string below. But can't seem to find any ways I can do it.

multiline_text = f'''
symbol={symbol},
margin_balance: {margin_balance},
margin_position:{margin_position},
margin_frozen: {margin_frozen}
'''

Expected output from this multiline string will be:

-----------------------
symbol=BTC,
margin_balance=0.15,
margin_position=0.05,
margin_available=0.01
----------------------
13
  • 3
    return is to return from a function, but you are not in one. Commented Jun 14, 2020 at 3:57
  • You are showing a set: {'symbol: BTC', 'margin_balance: 0.15', 'margin_position: 0.05', 'margin_frozen: 0.01'}. Did you want a dict: {'symbol': 'BTC', 'margin_balance': '0.15', 'margin_position': '0.05', 'margin_frozen': '0.01'}? Commented Jun 14, 2020 at 4:01
  • @tdelaney you're right. thanks for this. The function fixed it. Any idea on how to use the return text to create the multiline_text? Commented Jun 14, 2020 at 4:01
  • @tdelaney set is correct. generally the api call will send back a list of objects. Commented Jun 14, 2020 at 4:09
  • Oops, I thought you had a dict that should format multiline_text. I'll have to change my answer. Commented Jun 14, 2020 at 4:14

2 Answers 2

1

Create and print a long string

btc_positions=[{ 'symbol: BTC', 'margin_balance: 0.15', 'margin_position: 0.05', 'margin_frozen: 0.01'},
               { 'symbol: BTC2', 'margin_balance: 0.15', 'margin_position: 0.05', 'margin_frozen: 0.01'}]

def test(value):
    """
    value: list of sets of strings
    """
    for p in value:  # iterate list
        d = dict(s.split(': ') for s in p)  # turn sets of strings to dict
        d = sorted(d.items(), key=lambda x: x[1], reverse=True)  # reverse sort into list of tuples
        l = '-' * 15  # dashed line
        fin = f'{l}\n'  # create string to print 
        for v in d:
            fin += '='.join(v)
            fin += '\n'
        fin += l

        print(fin)


test(btc_positions)

---------------
symbol=BTC
margin_balance=0.15
margin_position=0.05
margin_frozen=0.01
---------------
---------------
symbol=BTC2
margin_balance=0.15
margin_position=0.05
margin_frozen=0.01
---------------

Print out the tuple pairs without creating a long string

  • Output looks the same as first function
def test(value):
    """
    value: list of sets of strings
    """
    for p in value:  # iterate list
        d = dict(s.split(': ') for s in p)  # turn set of strings to dict
        d = sorted(d.items(), key=lambda x: x[1], reverse=True)  # reverse sort
        l = '-' * 15  # dashed line
        print(l)
        for k, v in d:
            print(f'{k}={v}')  # print key value pairs
        print(l)

Return a string for messaging app

def test(value):
    """
    value: sets of strings
    """
    d = dict(s.split(': ') for s in p)  # turn set of strings to dict
    d = sorted(d.items(), key=lambda x: x[1], reverse=True)  # reverse sort
    l = '-' * 15  # dashed line
    fin = f'{l}\n'
    for v in d:
        fin += '='.join(v)
        fin += '\n'
    fin += l
    return fin

# iterate through btc_positions
for v in btc_positions:
    text = test(v)

    # send text to app
Sign up to request clarification or add additional context in comments.

Comments

1

You have a for loop, but loops don't need return statements. You can break to exit early, continue to go back to the top of the loop early, or just let the loop finish. All of the variables used in the loop are available to the code outside of the loop. Contrast that to a function where variables in the function are not available to the thing that calls the function. In your case, you could use an outer list to hold each formatted string as it is created in the loop.

The strings in your set are easy to split into name/value pairs. They can be used to create a dict which can then be used to format the output strings the way you want.

btc_positions=[{
'symbol: BTC', 
'margin_balance: 0.15',
'margin_position: 0.05',
'margin_frozen: 0.01'}]

# turn btc_positions set into dict for print parameters
btc_position_params = [
    dict(value.split(": ") for value in position)
    for position in btc_positions]


# create new list of formatted output
multiline_text_list = []
for pos in btc_position_params:
    multiline_text_list.append('''symbol={symbol},
margin_balance={margin_balance},
margin_position={margin_position},
margin_frozen={margin_frozen}'''.format(**pos))

# show result
for pos_text in multiline_text_list:
    print("--------------------")
    print(pos_text)
print("--------------------")

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.