Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I call a variable inside a fuction to the outsides in Python?

I have this code:

import sys

x=0
y=0
z=0

def Total(a,b,c):
  a=1
  b=2
  c=a+b
  return c

Total(x,y,z)
print(z)

I initially anticipated a result of 3, but unfortunately, the outcome is still 0. How can I resolve this issue?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You need to assign the returned value of Total to z. Right now you aren’t doing anything with the returned value of Total(x,y,z).

x=0
y=0
z=0

def Total(a,b,c):
    a=1
    b=2
    c=a+b
    return c

Total(x,y,z) # Total returns 3, but the value is not assigned to anything
print(z) # Z is still 0

What you need is:

x=0
y=0
z=0

def Total(a,b,c):
    a=1
    b=2
    c=a+b
    return c

z = Total(x,y,z) # Assign the returned value of Total (which is 3) to z
print(z) # Print z (which is 3) out
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading