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


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

(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.
[/quote]

Hi Buran,

I tried with same code which you mentioned but one of the validation fail while executing this.

def adds_numbers(x, y): 
    my_sum = x + y 
    return my_sum
my_sum = adds_numbers(10, 31) 
print(my_sum)
Validation1 passed :- Check if the variable my_sum has correct value

Test Output
41

Validation2 failed:- 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)

and if I adjust the indent inside I'm getting syntax error NameError: name 'my_sum' is not defined


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

Ah, OK
they complain because we use my_sum also inside the function and their check "thinks" it is hardcoded

def adds_numbers(x, y):
    return x + y
my_sum = adds_numbers(10, 31) 
print(my_sum)
or just use different name inside the function

def adds_numbers(x, y): 
    result = x + y 
    return result
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, 01:11 PM)buran Wrote: Ah, OK
they complain because we use my_sum also inside the function and their check "thinks" it is hardcoded

def adds_numbers(x, y):
    return x + y
my_sum = adds_numbers(10, 31) 
print(my_sum)
or just use different name inside the function

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

I tried both the options but validition2 got fail again

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)


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

Sorry, I really don't understand what the problem is. I checked all 3 solutions that we discuss here on regex101.com
their pattern my_sum\s*=\s*(10|31)\s*\+\s*(10|31) does not match any of them.
I don't know what they are after. Are you sure you don't have something like my_sum = 10 + 31 still in your code?


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

(Sep-24-2020, 01:34 PM)buran Wrote: Sorry, I really don't understand what the problem is. I checked all 3 solutions that we discuss here on regex101.com
their pattern my_sum\s*=\s*(10|31)\s*\+\s*(10|31) does not match any of them.
I don't know what they are after. Are you sure you don't have something like my_sum = 10 + 31 still in your code?

Hi Buran,

Yes I'm very-much sure there's no additional code as I commented all other lines except this code alone and executed.

I do have another scenario where the same kind of testing need to perform step by step as below but this code not written yet.

1. Define a function called print_money that takes two arguments: num1 and num2.

2. Inside the function print_money, define a variable called money_sum and set its value to adds_numbers(num1, num2).

3. Inside print_money, print out a value of the variable money_sum.

I