lets say i want to make a function that accepts a single argument that is supposed to be a number.

lets say the function displays the range between 0 and the argument.

Normally I'd do something like this:

def range_display(integer):
     set = []
     for num in range(0, integer):
          range_list = set.append(num)
          return range_list

But I don't know if thats how functions accept arguments. Just a guess.

How exactly do you make functions accept arguments? I don't quite get it...

Dani AI

Generated

The original posts show the right idea about parameters and arguments. Building on and , here are cleaner, more robust ways to produce and present a range of integers (examples target modern Python 3), plus a few quick pitfalls to watch for.

A simple, idiomatic function that returns the full list (so the caller decides how to display it):

def build_range(n: int) -> list:
    """Return a list of integers from 0 to n-1. Negative n -> empty list."""
    if not isinstance(n, int):
        raise TypeError("n must be an int")
    return list(range(max(0, n)))

if __name__ == "__main__":
    try:
        value = int(input("Enter an integer: "))
    except ValueError:
        print("Please enter a whole number.")
    else:
        print(build_range(value))

If you need a lazy sequence (no full-list allocation), return an iterator:

def iter_range(n: int):
    """Yield 0 .. n-1 lazily."""
    if not isinstance(n, int) or n <= 0:
        return
    for i in range(n):
        yield i

# usage: for item in iter_range(10): print(item)

Quick checklist and tips:

  • Prefer returning data from functions; print at the call site if needed.
  • Avoid shadowing built-ins (examples: list, dict, set, range, input).
  • Validate input and handle negative or non-integer arguments explicitly.
  • Use clear names, a short docstring, and if __name__ == "__main__": for script behavior.

These patterns keep the function small, testable, and easy to reuse.

Recommended Answers

All 11 Replies

def range_display(integer): # range_display(parameters)
     set = []
     for num in range(0, integer):
          range_list = set.append(num)
          return range_list

range_display(10) # range_display(arguments)

You seem to have it correct. When you define a function you set the "parameters" inside the "()". And when you call the function you supply the "arguments" to match the parameters. You access the arguments in the function just like variables.

def range_display(integer): # range_display(parameters)
     set = []
     for num in range(0, integer):
          range_list = set.append(num)
          return range_list

range_display(10) # range_display(arguments)

You seem to have it correct. When you define a function you set the "parameters" inside the "()". And when you call the function you supply the "arguments" to match the parameters. You access the arguments in the function just like variables.

If it's correct then how come I put this into a file and test it with idle and it outputs

None
None
None
None
None

When I enter 5

for this code:

def range_display(integer):
    set = []
    for num in range(0, integer):
        range_list = set.append(num)
        print range_list
        
integer = int(input('Enter an integer here -- > '))

range_display(integer)
input()

It's supposed to output:

[0,1,2,3,4]

When I said you have it correct I didn't mean your script would work. I just meant that you are setting parameters and arguments the right way.

Anyways I corrected it for you.

def range_display(integer):
    set = []
    for num in range(0, integer):
        set.append(num)
    return set

integer = int(input('Enter an integer here -- > '))

range = range_display(integer)
print range
input()

The reason you kept getting None was because set.append(num) does not return a value (*it returns None*). It simply appends the list "set".

Dont use set as variable

>>> set
<class 'set'>
>>> print (set.__doc__)
set(iterable) --> set object

Build an unordered collection of unique elements.
>>>

As you see it`s a python build in.

#python 3.x
def range_display(integer):
    my_set = []
    for num in range(0, integer):
        my_set.append(num)  #Dont make a variable inside for loop,you shall just append to my_set
    print (my_set)          #Move this to display result only one time

integer = int(input('Enter an integer here -- > '))  #input 10

range_display(integer)
'''
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
'''

That is a good point, I don't know why I didn't catch that.

That is a good point, I don't know why I didn't catch that.

Me neither, I guess it was pretty late and I was just thinkin 'a set of numbers' so I named the variable 'set' lol.

Nevermind, I'm still having the same problem with my new code:

def range_display(integer):
    s = []
    for num in range(0, integer):
        range_list = s.append(num)
        print range_list
        
integer = int(input('Enter an integer here -- > '))

range_display(integer)
input()

Your having the problem because you must not have read the entire thread. I fixed your problem a few posts ago...

When I said you have it correct I didn't mean your script would work. I just meant that you are setting parameters and arguments the right way.

Anyways I corrected it for you.

EDIT: changed the variable name so it doesn't use set

def range_display(integer):
    range_list = []
    for num in range(0, integer):
        range_list.append(num)
    print range_list

integer = int(input('Enter an integer here -- > '))

range_display(integer)

input()

The reason you kept getting None was because set.append(num) does not return a value (*it returns None*). It simply appends the list "set".

Are you using python 2.x or python 3.x?
Mine code is for python 3.x,because i did think you used python 3.x.

This line look like you use python 3.x
integer = int(input('Enter an integer here -- > '))

But here here look like you use python 2.x
print range_list

2.x, Now that you bring that line up though, it should be str(raw_input('Enter int: '))
I have to admit, I didn't nit pick this code when I first looked at it. I just got it to run and answered the problem. I should be more thorough next time.

str(raw_input('Enter int: '))

No raw_input return a string,so no need for str .
Input return a integer this is for python 2.x
Or for python 2.x you can use int(raw_input('return integer')) input python 3.x return a string.
Same as raw_input python 2.x
Python 3.x has only input ,not input and raw_input as python 2,x

So therefor is normal to use int(input('enter integer')) to get it to return a integer in python 3.x.

That`s why i did think ffs82defxp used python 3.x
But i get unsure when i did not see this line that only work for python 2.x print range_list for python 3.x off course this print (range_list) So ffs82defxp will clear this upp.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.