Functions

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 711 Views - Last Post: 05 January 2019 - 09:30 PM Rate Topic: -----

#1 machetech   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 02-January 19

Functions

Posted 02 January 2019 - 07:32 AM

Hey guys, I am knew in python, and am now learning functions. So I came across this question :
Write a function which returns a list of even numbers in a range of two numbers, if number let's say a is greater than b, else it returns a list of odd numbers.
Is This A Good Question/Topic? 0
  • +

Replies To: Functions

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14687
  • View blog
  • Posts: 58,690
  • Joined: 12-June 08

Re: Functions

Posted 02 January 2019 - 07:33 AM

Okay? What have you tried, thought about, or considered?
Was This Post Helpful? 0
  • +
  • -

#3 machetech   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 02-January 19

Re: Functions

Posted 02 January 2019 - 07:40 AM

This is what I have tried:
Def num(a, b ) :
  Evn = []
  Odd = []
    For I in range(a, b )  :
        If a>b and a % 2==0:
           Evn. Append (I)
           Return evn
        Else:
           Odd. Append (i)
Num(10,3)
:code:
The code runs but return nothing

This post has been edited by modi123_1: 02 January 2019 - 07:46 AM
Reason for edit:: In the future, please use the [code] tag button in the editor.

Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14687
  • View blog
  • Posts: 58,690
  • Joined: 12-June 08

Re: Functions

Posted 02 January 2019 - 07:48 AM

I'll ask this - is python a case sensitive or insensitive language?
Was This Post Helpful? 0
  • +
  • -

#5 machetech   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 02-January 19

Re: Functions

Posted 02 January 2019 - 07:52 AM

sure will thanks, but what do you think about my code?

it is case sensitive,pardon me, i am also new here
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14687
  • View blog
  • Posts: 58,690
  • Joined: 12-June 08

Re: Functions

Posted 02 January 2019 - 07:55 AM

Then I would suggest correcting the multitude of case sensitive issues in that code.
Was This Post Helpful? 1
  • +
  • -

#7 machetech   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 02-January 19

Re: Functions

Posted 02 January 2019 - 08:07 AM

this is the actual code
def num(a,B)/>:
   evn = []
   odd = []
   for i in range (a,B)/>:
     if a > b and a % 2 == 0:
        evn.append(i)
        return evn
     else:
         odd.append(i)
         return odd
num(10,3)

       



b is lower case in the whole code
Was This Post Helpful? 0
  • +
  • -

#8 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2706
  • View blog
  • Posts: 4,315
  • Joined: 21-June 11

Re: Functions

Posted 02 January 2019 - 08:13 AM

The function will definitely return something (though not what you want since you never add more than one item to the list since you return on the first iteration of the loop). The reason that you're not seeing any output is that you don't do anything with the return value. In order to see the return value, you should call print when you call the function on line 11. Like this:

print(num(10,3))

Was This Post Helpful? 1
  • +
  • -

#9 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 499
  • View blog
  • Posts: 1,562
  • Joined: 27-December 13

Re: Functions

Posted 02 January 2019 - 08:26 AM

One more hint:
You need to consider the 'range(a, b )' function; what will this function return if a > b ?

The 'b ) ' becoming 'B )/>' is an error on the DIC page, - you can add a space between 'b' and ')' to counteract this.

This post has been edited by DK3250: 02 January 2019 - 08:26 AM

Was This Post Helpful? 1
  • +
  • -

#10 machetech   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 02-January 19

Re: Functions

Posted 02 January 2019 - 08:37 AM

View PostDK3250, on 02 January 2019 - 08:26 AM, said:

One more hint:
You need to consider the 'range(a, b )' function; what will this function return if a > b ?

The 'b ) ' becoming 'B )/>' is an error on the DIC page, - you can add a space between 'b' and ')' to counteract this.

it is suppose to return even numbers and vice verser if the opposote is true

View Postsepp2k, on 02 January 2019 - 08:13 AM, said:

The function will definitely return something (though not what you want since you never add more than one item to the list since you return on the first iteration of the loop). The reason that you're not seeing any output is that you don't do anything with the return value. In order to see the return value, you should call print when you call the function on line 11. Like this:

print(num(10,3))

thanks, let me do that
Was This Post Helpful? 0
  • +
  • -

#11 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2706
  • View blog
  • Posts: 4,315
  • Joined: 21-June 11

Re: Functions

Posted 02 January 2019 - 08:43 AM

View Postmachetech, on 02 January 2019 - 04:37 PM, said:

it is suppose to return even numbers and vice verser if the opposote is true


DK3250 didn't mean what your function is supposed to return, but what the range function returns. That is, what does e.g. range(10, 3) return? Answer: an empty iterable. So if a >= b the contents of your loop never run.

Incidentally, I was wrong before: There is a way for your function to return nothing and this is it.
Was This Post Helpful? 0
  • +
  • -

#12 machetech   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 02-January 19

Re: Functions

Posted 02 January 2019 - 08:55 AM

thanks alot, after iusing the print function, it gives me none.
can you please go through my code and maybe help me identify where the problem is?
Was This Post Helpful? 0
  • +
  • -

#13 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2706
  • View blog
  • Posts: 4,315
  • Joined: 21-June 11

Re: Functions

Posted 02 January 2019 - 09:15 AM

The reason that you're getting None is what DK3250 was trying to tell you: range(10, 3) is empty, so the loop body does not run.

Consider this:

def f():
  for x in [1,2,3]:
    return x

  return 42



This will return 1, right? Because when the loop body runs for the first time, return x is executed with x=1, making the function return with that value.

Now if we make the list empty like this:

def f():
  for x in []:
    return x

  return 42



Now it will return 42 because the return x never executes since there is no item in [] - it's empty. So it will directly go to the code after the loop, i.e. return 42. Now let's remove the return 42:

def f():
  for x in []:
    return x



Now it will still directly go to the code after the loop. But there's no return statement there. So instead, it will return Nothing because that's what Python functions return when they reached the end without a return statement.

And that's exactly what's happening in your code because range(10, 3) returns an empty iterable (in other words list(range(10,3)) == []), so for x in range(10, 3): acts exactly the same way as for x in []:.
Was This Post Helpful? 2
  • +
  • -

#14 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14687
  • View blog
  • Posts: 58,690
  • Joined: 12-June 08

Re: Functions

Posted 02 January 2019 - 09:15 AM

Review range and the 'step' parameter.
https://www.python-c...eu/for_loop.php
Was This Post Helpful? 0
  • +
  • -

#15 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 499
  • View blog
  • Posts: 1,562
  • Joined: 27-December 13

Re: Functions

Posted 03 January 2019 - 12:45 AM

@machetech: First, let me (a little late) welcone you to Dream.In.Code; it is a great place with a lot of helpful people.
I hope you are not 'scared away' by the long thread that evolved yesterday... :rolleyes:

Let me go back to your code in post #7 an add some comments.
You should use a consistent number of spaces for indentation; in this small code piece you use 2, 3 and 4 spaces, this will soon bring you into problems. The recommended indentation is 4 spaces.

The big thing in your task is whether a > b or a < b, resulting in a list of even or odd numbers. For completion a == b should also be handled gracefully. In code:
def num(a, b ):
    if a > b:
        # make the list of even numbers
    elif a < b:
        # make list of uneven numbers
    else:  # a == b
        # handle special case


You only produce one list - either even or odd - so, let's make a common list named 'result'.
If a < b the range function can be used directly: range(a, b )
If, however, a > b, you need to change the sequence: range(b, a)

This bring us to this (incomplete) code:
def make_list(a, b ):
    result = []
    if a > b:
        # make the list of even numbers

    elif a < b:  # make the list of uneven numbers
        for num in range(a, b ):
            if num % 2 == 1:  # identify the uneven numbers
                result.append(num)

    else:  # a == b
        # handle special case

    return result


I have changed the name of the function to a more 'telling' name; this is good practice and often helpful.
I did not make all code, hopefully you can do this yourself. Otherwise, just come back here.
It is up to you what to do if a == b, I would add a print statement and return the empty list.

I hope you find this helpful, give it a try and show us your result.
Happy Coding.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2