Python Forum
i don't understand how to use a variable outside of a function?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
i don't understand how to use a variable outside of a function?
#1
from this function:
def test(arg)
a = 0
if arg >3:
    a=1
return a
how do i use it in another function?:

def main():
    arg = rand.randrange(0,7)
    test(arg)
    if a = 1:
        print('I don't know what i'm doing')
Reply
#2
Pass as an attribute
>>> def func1():
...     return 15
...
>>> def func2(value):
...     print('Value x 2 = {}'.format(value * 2))
...
>>> def main():
...     a = func1()
...     func2(a)
...
>>> main()
Value x 2 = 30
Reply
#3
based on your code:
import random

def test(arg):
    if arg > 3:
        a = 1
    else:
        a = 0
    return a

def main():
    arg = random.randrange(0,7)
    a = test(arg) # here you can use any name, not neccessary to be a
    if a == 1:
        print("I don't know what i'm doing")
more pythonic would be

import random
def test(arg):
    return arg >3

def main():
    arg = random.randrange(0,7)
    if test(arg):
        print("I don't know what i'm doing")
also note the double quotes of the string - otherwise you need to escape the single quotes
Reply
#4
so in my case it would be:
def test(arg)
    a = 0
    if arg > 5:
        a = 1
   return a

def main():
    arg = random.randrange
    z = test(arg)
    if z ==1:
        Print('Will this work?')
Thank both of you , now i think i can grasp functions.
Reply
#5
here is our tutorial https://python-forum.io/Thread-Basic-Functions
Reply
#6
(Sep-30-2017, 08:16 PM)Rius2 Wrote: so in my case it would be:
arg = random.randrange

Well, no.  You never call random.randrange, so arg is just a reference to the function itself.  So "Will this work" should never be printed.
Reply
#7
Any function returns an object which you can assign to a variable.
So you define one. Simplified because I am lazy today:
def test(arg):
    return a
Then define another:
def second(some_argument):
    b = test(some_argument)
    if b != 'stupid example':
        return 'Good enough'
Now call 'second':
s = 'No?'
second(s)
You can pass any argument to a function and use it inside another one. some_argument could be any python object, however, it is called. Of course, its type has to be suitable to use it at all.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Don't Understand Recursive Function muzikman 10 6,091 Mar-08-2025, 01:58 PM
Last Post: bterwijn
  not able to call the variable inside the if/elif function mareeswaran 3 563 Feb-09-2025, 04:27 PM
Last Post: mareeswaran
  Unable to understand the function string.split() Hudjefa 8 2,477 Sep-16-2024, 04:25 AM
Last Post: Pedroski55
  Variable for the value element in the index function?? Learner1 8 2,935 Jan-20-2024, 09:20 PM
Last Post: Learner1
  Variable is not defined error when trying to use my custom function code fnafgamer239 4 1,605 Nov-23-2023, 02:53 PM
Last Post: rob101
  Printing the variable from defined function jws 7 7,405 Sep-03-2023, 03:22 PM
Last Post: deanhystad
  Function parameter not writing to variable Karp 5 2,143 Aug-07-2023, 05:58 PM
Last Post: Karp
  Retrieve variable from function labgoggles 2 1,807 Jul-01-2022, 07:23 PM
Last Post: labgoggles
  Cant transfer a variable onto another function KEIKAS 5 2,934 Feb-09-2022, 10:17 PM
Last Post: deanhystad
  Please explain uncommon way of declaring and using variable [function.variable] esphi 4 3,287 Nov-07-2020, 08:59 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020