Python Forum
Python variable and function usage at a time - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Python variable and function usage at a time (/thread-29869.html)

Pages: 1 2


Python variable and function usage at a time - prasanthbab1234 - Sep-23-2020

Create a variable called my_sum that has a value of 10 + 31. However, instead of adding the numbers directly in my_sum, set my_sum to be the result of your adds_numbers function.

def my_sum(adds_numbers):
    my_sum = x + y
    return my_sum
adds_numbers= 10+31
print(adds_numbers)
but something is missing in this so could someone give me an idea about it.


RE: Python variable and function usage at a time - jefsummers - Sep-23-2020

You have things backwards. Create a VARIABLE named mysum. You created a function. It should use the FUNCTION adds_numbers(). You have a variable named adds_numbers.


RE: Python variable and function usage at a time - prasanthbab1234 - Sep-23-2020

(Sep-23-2020, 11:35 PM)jefsummers Wrote: You have things backwards. Create a VARIABLE named mysum. You created a function. It should use the FUNCTION adds_numbers(). You have a variable named adds_numbers.

Hi Thanks for your prompt reply but my doubt is if we declare variable outside the function how do we call it here? could you give me some idea on the same?

my_sum= 10 + 31
def adds_numbers():
    my_sum = (adds_numbers(x+y))
    return my_sum
adds_numbers= 10+31
print(adds_numbers)
even this is not pass through.


RE: Python variable and function usage at a time - prasanthbab1234 - Sep-24-2020

I tried as below and it has small issue it seems.

def adds_numbers(x, y): 
    my_sum = x + y 
    return my_sum
    my_sum = adds_numbers(10, 31) 
print(my_sum)



RE: Python variable and function usage at a time - buran - Sep-24-2020

line#4 must be outside the function
def adds_numbers(x, y): 
    my_sum = x + y 
    return my_sum
my_sum = adds_numbers(10, 31) 
print(my_sum)



RE: Python variable and function usage at a time - prasanthbab1234 - Sep-24-2020

(Sep-24-2020, 06:29 AM)prasanthbab1234 Wrote: I tried as below and it has small issue it seems.

def adds_numbers(x, y): 
    my_sum = x + y 
    return my_sum
    my_sum = adds_numbers(10, 31) 
print(my_sum)

Hi Buran,

I'm new to this forum so excuse me if there's any issue in my post as I copy paste the code from code editor and not sure it won't copied here properly and I tried with given format and I got following error
message saying my_sum not hardcoded so please explain how to hard code?

def adds_numbers(x, y): 
    my_sum = x + y 
    return my_sum
    my_sum = adds_numbers(10, 31) 
print(my_sum)
[/quote]


RE: Python variable and function usage at a time - ebolisa - Sep-24-2020

Try this:_

def adds_numbers(x, y):
    my_sum = x + y
    return my_sum

my_sum = adds_numbers(10, 31)
print(my_sum)



RE: Python variable and function usage at a time - buran - Sep-24-2020

(Sep-24-2020, 12:25 PM)prasanthbab1234 Wrote: I got following error
message saying my_sum not hardcoded
It would say that my_sum is not defined, not harcoded.
All use of my_sum on lines 2-4 is inside the function i.e. it's a variable inside the scope of the functio
my_sum on line 5 is in global scope, i.e. that is different variable than the ones inside the function.
If you move line 4 outside the function as I show you, it's a different matter. In my code on line 4 result returned from the function is assigned to my_sum and then on line 5 you can print it (it's defined)


RE: Python variable and function usage at a time - prasanthbab1234 - Sep-24-2020

(Sep-24-2020, 12:25 PM)ebolisa Wrote: Try this:_

def adds_numbers(x, y):
    my_sum = x + y
    return my_sum

my_sum = adds_numbers(10, 31)
print(my_sum)

I tried with both options and issue remains same as we need to hard code my_sum but not sure how?


RE: Python variable and function usage at a time - buran - Sep-24-2020

(Sep-24-2020, 12:44 PM)prasanthbab1234 Wrote: as we need to hard code my_sum but not sure how?
It's unclear what you want. Is this homework? Post the text of the assignment verbatim.
Your initial request was clear:
(Sep-23-2020, 11:18 PM)prasanthbab1234 Wrote: instead of adding the numbers directly in my_sum, set my_sum to be the result of your adds_numbers function.