0

Variable with function in python

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) Something I missed in this so help me to understand.

23rd Sep 2020, 11:25 PM
Abhi
Abhi - avatar
8 odpowiedzi
+ 4
Maybe this is what they want. def adds_numbers(x, y): return x + y def my_sum(x, y): return adds_numbers(x, y) print(my_sum(10, 31))
24th Sep 2020, 1:12 AM
David Ashton
David Ashton - avatar
+ 3
Abhi def my_sum(x,y): my_sum = x + y return my_sum print(my_sum(10,31)) ## output: 41 https://code.sololearn.com/c1kYMtRMpubp/?ref=app
24th Sep 2020, 12:16 AM
BroFar
BroFar - avatar
+ 2
You were very close! Your function needs to be named adds_numbers. And your function needs to take in two arguments, in this case, x and y. def adds_numbers(x, y): my_sum = x + y return my_sum print(adds_numbers(10, 31))
24th Sep 2020, 12:27 AM
Marina
Marina - avatar
+ 1
The correct way of doing this from the way i see it is: def adds_numbers(x, y): # Define function with the name adds_numbs my_sum = x + y # Put variable my_sum that is the sum of x and y return my_sum # We return the value of my_sum to the main program print(adds_numbers(10, 31)) # Print the function using 10 and 31 in place of x and y. This would be the correct way assuming my_sum should be part of the adds_numbers function
24th Sep 2020, 12:04 AM
Carlos
Carlos - avatar
0
Hi Folks, Thanks for your prompt response but I got below reply while testing the given code def adds_numbers(x,y): my_sum = x+y return my_sum print(adds_numbers(10,31)) Note:- So close! Take another look at the instructions — you've almost got it! Your code should look something like my_sum = adds_numbers(______) Error:- ERROR: test_unit_test (__main__.UnitTests) ---------------------------------------------------------------------- Traceback (most recent call last): File "/root/sandbox/nt-test-1b159b5c.py", line 5, in test_unit_test self.assertEqual(my_sum, 41) NameError: name 'my_sum' is not defined error 2:- Checking if the my_sum variable is not hard-coded. error 3:- Create a variable my_sum
24th Sep 2020, 5:04 AM
Abhi
Abhi - avatar
0
Try this: def adds_numbers(x, y): add = x + y return add my_sum = adds_numbers(10, 31) print(my_sum)
24th Sep 2020, 5:33 AM
Marina
Marina - avatar
0
Hi Marina, Thanks for your prompt response I guess one more issue with given syntax Error:- Checking if the my_sum variable is not hard-coded. Description Searched your code for a specific pattern: my_sum\s*=\s*(10|31)\s*\+\s*(10|31)
24th Sep 2020, 5:49 AM
Abhi
Abhi - avatar
0
No problem, Abhi! I'm really not sure why it's giving a syntax error. I'm hoping the print statement isn't throwing anything off. I feel like you're so close to the right answer, but I'm not sure exactly what the grader is looking for.
24th Sep 2020, 5:58 AM
Marina
Marina - avatar