0
a=1

def choice():
    if a==1 :
        return 'more work to do '
        b='cool'
    else :
        return 'more work to do '
        b='not cool'

print (b)

all I want is to call it outside the function in another much longer code . this is the simple version. thanks a lot.

  • Does this answer your question? [Using global variables in a function](https://stackoverflow.com/questions/423379/using-global-variables-in-a-function) – MagnusO_O Dec 26 '22 at 22:00

4 Answers4

1

You could make b a global variable and use the global keyword to modify it within choice. But please don't.

>>> a = 1
>>> b = None
>>> def choice():
...   global b
...   if a == 1: b = "cool"
...   else: b = "not cool"
...
>>> choice()
>>> b
'cool'
>>>
Chris
  • 26,361
  • 5
  • 21
  • 42
  • well . it worked . thanks .but why don't you recommend it – Younes Faiz Dec 26 '22 at 22:02
  • There are many good reasons. At least for me it boils down to good code being code that's easy to reason about in small pieces. Global variables mean that you have to consider the whole program as one piece and can't break things down into smaller problems. – Chris Dec 26 '22 at 22:06
  • I tried to write code that's easy to reason about in small pieces . and the program is running as intended . but i decided to add small details to personalize it . that's where I got stuck . it's just a course exercise by the way . a dictionary in tkinter . – Younes Faiz Dec 26 '22 at 22:11
  • @YounesFaiz using mutable global state (which is what the `global` statement allows you to do) is a *classic* anti-pattern in all programming languages. – juanpa.arrivillaga Dec 26 '22 at 22:34
  • @juanpa.arrivillaga thanks. I'll keep it in mind . all I'm storing in it is a word . that was retrieved from a json file . – Younes Faiz Dec 26 '22 at 23:24
0

As you've seen, b is a local variable inside choice and can't be referenced outside it. One option is to return the value from choice:

def choice():
    if a == 1:
        return 'cool'
    else:
        return 'not cool'

b = choice()
print(b)
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • the thing is I already retuned a value to the function before setting a value to b in the function . the b is second value i care about – Younes Faiz Dec 26 '22 at 21:56
  • 3
    @YounesFaiz Then you should show representative code. You *can* return two items via a tuple, e.g. `return a,b`. – Mark Tolonen Dec 26 '22 at 21:57
  • how to do it . I'm still learning . i'm about 2 weeks of taking a course in my free time . thanks a lot for ur feedback. – Younes Faiz Dec 26 '22 at 22:05
0

There are 2 ways of achieving this. First of all you can just simply return b along with 'more work to do' like this:

a=1

def choice():
    if a==1 :
        b = 'cool'
        return 'more work to do', b
    else :
        b = 'not cool'
        return 'more work to do', b

moreWorkToDo, b = choice()
print (b)

Another way is to set b as a global variable however it is usually not advised. Global variables can be accessed and modified from anywhere in the code, which can make it difficult to trace the source of a bug. Anyways here is the code:

a=1

def choice():
    global b
    if a==1 :
        b = 'cool'
        return 'more work to do'
    else :
        b = 'not cool'
        return 'more work to do'

moreWorkToDo = choice()
print (b)
Jacob G
  • 1
  • 2
0
"""
See if the following meets your objective.
It may be more advanced than what you have learned so for.
A function can return multiple values.
"""
a = 1


def choice():
    if a == 1:
        b = 'cool'  # assign the value to b
        # the following data structure is a tuple.
        # That is multiple items separated by commas
        return 'more work to do ', b
    else:
        b = 'not cool'  # assign the value to b
        # the following data structure is a tuple.
        # That is multiple items separated by commas
        return 'more work to do ', b


# work_status, b_value is a tuple of names just waiting for values
work_status, b_value = choice()  # choice returns a tuple
# work_status, b_value = choice() is called unpacking values
print(f'{work_status=} {b_value=}')
 
# print (b)
Carl_M
  • 861
  • 1
  • 7
  • 14