Printing Variable In Function

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1133 Views - Last Post: 04 December 2015 - 08:47 AM Rate Topic: -----

#1 Traxxasfan   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 42
  • Joined: 06-May 15

Printing Variable In Function

Posted 01 December 2015 - 09:10 AM

Hi all,

I am having trouble printing a variable that is inside a function. I have tried returning the variable, making the variable a global variable, and yet nothing. What should I do? I don't know how to proceed from here. Below is my code:

import cmath
newInput = raw_input()
dim1, dim2, dim3 = newInput.split()
float(dim1)
float(dim2)
float(dim3)
def sFinder():
    s2 = dim1 + dim2 + dim3
    s1 = float(s2) / 2
    s = float(s1)
    float(s)
    return s
def heron():
    ans2 = s((s-dim1)(s - dim2)(s - dim3))
    ans = sqrt(ans2)
    return ans
print ans



Is This A Good Question/Topic? 0
  • +

Replies To: Printing Variable In Function

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 16479
  • View blog
  • Posts: 65,313
  • Joined: 12-June 08

Re: Printing Variable In Function

Posted 01 December 2015 - 09:32 AM

A large part comes from scope.
You try to print 'ans' here:
17	print ans


.. but 'ans' only exists in this function here as some sort of return value.
13	def heron():
14	    ans2 = s((s-dim1)(s - dim2)(s - dim3))
15	    ans = sqrt(ans2)
16	    return ans



Also remember to assign a value to a variable you need to use the 'SET' (typically the '=') else conversions like this have no meaning.

04	float(dim1)
05	float(dim2)
06	float(dim3)



The same goes for functions. If you call a function that has a return value I would hope you are catching that value some where!
Was This Post Helpful? 0
  • +
  • -

#3 Traxxasfan   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 42
  • Joined: 06-May 15

Re: Printing Variable In Function

Posted 01 December 2015 - 10:11 AM

I do have the dim1, dim2, and dim3 equal to something. What happens is the user inputs 3 numbers, separated by a space, then the three different numbers are broken down into three different variables (dim1, dim2, dim3), and then I have to do some math stuff from there. I have some updated code below, but what if I used a global variable instead of trying to return a value? Would it do the same thing?

import cmath
newInput = raw_input()
dim1, dim2, dim3 = newInput.split()
float(dim1)
float(dim2)
float(dim3)
def sFinder(s):
    s2 = dim1 + dim2 + dim3
    s1 = float(s2) / 2
    float(s1)
    return s1
def heron(answer):
    ans2 = s((s-dim1)(s - dim2)(s - dim3))
    ans = sqrt(ans2)
    return ans
print answer


Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 16479
  • View blog
  • Posts: 65,313
  • Joined: 12-June 08

Re: Printing Variable In Function

Posted 01 December 2015 - 10:15 AM

Quote

I do have the dim1, dim2, and dim3 equal to something.


Except this returns strings:
03	dim1, dim2, dim3 = newInput.split()


.. and these do _NOT_ convert them to floats since you do not assign that conversion anywhere.
04	float(dim1)
05	float(dim2)
06	float(dim3)


Example:

foo = '5 4 6'
bar1, bar2, bar3 = foo.split()

print type(bar1)
print type(bar2)
print type(bar3)

bar1 = float(bar1)
bar2 = float(bar2)
bar3 = float(bar3)

print type(bar1)
print type(bar2)
print type(bar3)



<type 'str'>
<type 'str'>
<type 'str'>
<type 'float'>
<type 'float'>
<type 'float'>

Was This Post Helpful? 0
  • +
  • -

#5 Traxxasfan   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 42
  • Joined: 06-May 15

Re: Printing Variable In Function

Posted 01 December 2015 - 10:17 AM

Ohhhh I get it now. And I guess I should either read up on functions or just try something different because I really don't know how the whole return thing works or anything.
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 16479
  • View blog
  • Posts: 65,313
  • Joined: 12-June 08

Re: Printing Variable In Function

Posted 01 December 2015 - 10:21 AM

Return is not that hard of a concept. A function goes back to where it is called and says "here - here's some data for you". If you do not have a variable to have that data assigned then it goes no where. Like if you give your friend a block of wood, he does some work to it, and wants to give it back to you.. If you are not there to get it then the work done to the wood goes unused.
Was This Post Helpful? 0
  • +
  • -

#7 Traxxasfan   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 42
  • Joined: 06-May 15

Re: Printing Variable In Function

Posted 01 December 2015 - 10:22 AM

Oh okay, so I should create the variable "answer" outside of the function for the function to return that value correctly? And should I do the same for the "s" variable too?
Was This Post Helpful? 0
  • +
  • -

#8 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 16479
  • View blog
  • Posts: 65,313
  • Joined: 12-June 08

Re: Printing Variable In Function

Posted 01 December 2015 - 10:23 AM

If you want to use the values coming out of the functions then yes.. you should.
Was This Post Helpful? 0
  • +
  • -

#9 Traxxasfan   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 42
  • Joined: 06-May 15

Re: Printing Variable In Function

Posted 01 December 2015 - 10:28 AM

Okay, so I set the "answer" and "s" values both to 0.0 before they are called. Then, when I input "1 1 1", the program only returns "0.0" which is what I have both of those variables set to. I think I have everything successfully floated too now, but most likely not. Here is the updated code.

import cmath
answer = 0.0
s = 0.0
newInput = raw_input()
dim1, dim2, dim3 = newInput.split()
dim1 = float(dim1)
dim2 = float(dim2)
dim3 = float(dim3)
def sFinder(s1):
    s2 = dim1 + dim2 + dim3
    s1 = float(s2) / 2
    s1 = float(s1)
    return s1
def heron(answer):
    ans2 = s((s-dim1)*(s - dim2)*(s - dim3))
    ans = sqrt(ans2)
    return answer
print answer


Was This Post Helpful? 0
  • +
  • -

#10 andrewsw   User is offline

  • no more Mr Potato Head
  • member icon

Reputation: 6957
  • View blog
  • Posts: 28,696
  • Joined: 12-December 12

Re: Printing Variable In Function

Posted 01 December 2015 - 10:32 AM

You never call - use - your functions sFinder or heron. You've defined them but never call them, e.g. something = sFinder(something_else).

What tutorial or book are you studying?
Was This Post Helpful? 0
  • +
  • -

#11 Traxxasfan   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 42
  • Joined: 06-May 15

Re: Printing Variable In Function

Posted 01 December 2015 - 10:41 AM

I was using CodeAcademy to prepare for a programming competition Ive entered in, but I have found other instances where they weren't aligned with real world practice. Okay, so I added code to call them, but my program still returns "0.0". I added a print to see if the first function was doing anything and it seems it is not, but here is the code:

import cmath
answer = 0.0
s = 0.0
newInput = raw_input()
dim1, dim2, dim3 = newInput.split()
dim1 = float(dim1)
dim2 = float(dim2)
dim3 = float(dim3)
def sFinder(s):
    s2 = dim1 + dim2 + dim3
    s1 = float(s2) / 2
    s = float(s1)
    return s
def heron(answer):
    ans3 = (s-dim1)*(s - dim2)*(s - dim3)
    ans2 = s * ans3
    ans = sqrt(ans2)
    return answer

sFinder
heron
print s
print answer


Was This Post Helpful? 0
  • +
  • -

#12 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 16479
  • View blog
  • Posts: 65,313
  • Joined: 12-June 08

Re: Printing Variable In Function

Posted 01 December 2015 - 10:45 AM

Again your variables need to be used to GET the return value. How do you set a variable's value?
Was This Post Helpful? 0
  • +
  • -

#13 Traxxasfan   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 42
  • Joined: 06-May 15

Re: Printing Variable In Function

Posted 01 December 2015 - 10:46 AM

I set the variables by setting them equal to 0.

answer = 0.0
s = 0.0

Was This Post Helpful? 0
  • +
  • -

#14 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 16479
  • View blog
  • Posts: 65,313
  • Joined: 12-June 08

Re: Printing Variable In Function

Posted 01 December 2015 - 10:49 AM

You can use that same logic to assign a function's return value to the variable.

<variable> = <function>

Was This Post Helpful? 1
  • +
  • -

#15 Traxxasfan   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 42
  • Joined: 06-May 15

Re: Printing Variable In Function

Posted 01 December 2015 - 10:55 AM

Okay, thank you very much. Now I think I just need to fix my formula, but otherwise it works. Thank you very much.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2