Python Forum
Python variable and function usage at a time
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python variable and function usage at a time
#1
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.
Reply
#2
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.
Reply
#3
(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.
Reply
#4
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)
Reply
#5
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)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
(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]
Reply
#7
Try this:_

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

my_sum = adds_numbers(10, 31)
print(my_sum)
Reply
#8
(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)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#9
(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?
Reply
#10
(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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  usage of ":" zeppos 1 479 Dec-12-2023, 10:11 AM
Last Post: buran
  Simple animation -- time evolution of function schniefen 0 994 Nov-20-2022, 08:05 PM
Last Post: schniefen
  New to Python - tiny coding assistance on user input function and assign to variable Mountain_Duck 1 2,465 Mar-23-2019, 06:54 PM
Last Post: Yoriz
  Creating a variable that displays time groovydingo 3 2,857 Apr-17-2018, 04:46 AM
Last Post: tannishpage

Forum Jump:

User Panel Messages

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