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
#11
(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
Reply
#12
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)
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
#13
(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)
Reply
#14
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?
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
#15
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  usage of ":" zeppos 1 507 Dec-12-2023, 10:11 AM
Last Post: buran
  Simple animation -- time evolution of function schniefen 0 1,020 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,497 Mar-23-2019, 06:54 PM
Last Post: Yoriz
  Creating a variable that displays time groovydingo 3 2,892 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