Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
help with list
#21
(Jan-31-2019, 04:57 PM)perfringo Wrote:
(Jan-31-2019, 03:29 PM)sonedap Wrote: well,i am studying Functions now and i still don't get how they work in general.
Also,i don't understand how this one function works..

Maybe following can help:

There are numerous built-in functions in Python. You used number of them in your code: int, print, range. So using functions and providing arguments to functions should be easy-peasy for you.

If there is something you want to accomplish and it is not possible with using built-in functions and functions in built-in modules / third party modules then you can define functions yourself.

Functions are useful for two main reasons: (1) you can reuse them and (2) you can separate what is done from how it's done. Imagine, that every time you want something print you would have to write instead print('something') pretty lengthy and possibly complicated code and you get the picture.

Function I wrote is quite similar to the code you provided in your snippet.

To walk you through this code by rows:

def validate(request, allowed_range):
    """Return input in allowed_range
     
    :param request: request made to user
    :type request: str
    :param allowed_range: allowed numbers range
    :type allowed_range: range
    :return: user input within allowed range
    :rtype: int
    """
    m = (f'Expected integer in range {min(allowed_range)} - {max(allowed_range)} '
         f'but input was')
      
    while True:
        answer = input(request)
        try:
            answer = int(answer)
            if answer in allowed_range:
                return answer
            raise ValueError
          
        except ValueError:
            print(f'{m} {answer}') 
# 1 - defining function by using keyword def, giving it descriptive name following PEP8 and descriptive parameter names. These parameters names are arbitrary and have nothing to do with arguments you will actually use when calling the function (read more: What is the difference between arguments and parameters?). Parameters are defined here so that different requests and ranges can be used when calling function.

# 2 - 10 - docstring which describes what the function does and what types of arguments it accepts. Docstring serves two primery objectives: to give others understanding what function does and to remind yourself what you did. Believe me, if you return to function you wrote couple of months ago it seems that somebody else wrote it. For that reason it advisable to document your function even if it is not meant for others. You can access this docstring like with built-in functions by help(function_name). If this function is in your namespace then you can do this:

>>> help(validate)
validate(request, allowed_range)
    Return input in allowed_range
    
    :param request: request made to user
    :type request: str
    :param allowed_range: allowed numbers range
    :type allowed_range: range
    :return: user input within allowed range
    :rtype: int


# 11-12 - defining part of message to display when user enters values which are not in allowed range. To assist user, it takes max and min values from range to indicate within which range input should be. This could be done in except block but for (subjective) readability purposes (line is long and print statement is intented 8 spaces) it is created separately. F-strings are used (requires Python 3.6 or newer).

# 14-23 - while-loop is quite similar to code you provided. Differences are: user input is assigned to name 'answer' before try..except [#15]. This way it is (subjectively) more readable and usable for message in except-block; if user input is not int (actually string which can be converted to string) [#17] or if it is int but not in allowed range [#20] ValueError will be raised. If input is in accepted range it returns user input as int [#19].

# 22-23 - if ValueError occurs in try-block (either from input not being int or not in allowed range) message will be printed which states allowed range and what user entered.

while loop executes until int is returned.

After defining function you can use it. When calling function in these particular cases arguments are: 'Enter number a in range 100-120: ' ; 'Enter number b in range 3-7: ' (request) and range(100, 121) ; range(3, 8) (allowed_range)

First of all,i want to thank you for the time you spend to explain all these to me.
Its just that i have one month to learn how Python works (university student) and that's why i am asking much.Forgive me if i have many queries.

i will start a new topic for classes later so that yu can explain classes a little better than my professor. Smile
Reply
#22
(Feb-01-2019, 07:56 AM)sonedap Wrote: i will start a new topic for classes later so that yu can explain classes a little better than my professor. Smile

We've made a whole series of tutorials on classes here on the site. Start with this one.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#23
(Feb-01-2019, 03:52 PM)ichabod801 Wrote:
(Feb-01-2019, 07:56 AM)sonedap Wrote: i will start a new topic for classes later so that yu can explain classes a little better than my professor. Smile

We've made a whole series of tutorials on classes here on the site. Start with this one.

i will study them.Thanks a lot!!!!!!!!!!!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020