#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    7
    Rep Power
    0

    How do I use Def and functions


    Hi all sorry I am totally new at not only python but also posting tried to change my Tread title but couldn't. Should have read the new user post first.

    The program needs to do the following:
    The user inputs a number any number then the program gives the even numbers up to the number input. So if you input 10 the result will be 0,2,4,6,8,
    This is what I have so far but it keeps returning something else. I have to use this format Def then function then result

    PHP Code:
    def evennum(i):
         for 
    i in range(0,20,2):
            
    evenrun = (0,int (user_num),2)

         return 
    evenrun

    user_num 
    input ("please enter number:")
    num_resolt evennum (int (user_num))
    print(
    "The result of"user_num"is",num_resolt
    please any help will be appreciated.
    Last edited by Alf4JC; July 18th, 2013 at 09:30 PM. Reason: Bad beginner post
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Location
    Usually Japan when not on contract
    Posts
    240
    Rep Power
    12
    First, I'm not going to do your homework for you, but I will point you in the right direction.

    Second, try using the "python" highlight tag instead of the PHP one; different language and all... (you can just write highlight="python" in square brackets to get this also).

    On to your problem:
    python Code:
    def evennum(i):
        for i in range(0,20,2):
            evenrun = (0,int (user_num),2)
        return evenrun
     
    user_num = input ("please enter number:")
    num_resolt = evennum (int (user_num))
    print("The result of", user_num, "is",num_resolt)

    Check out line 3. You're declaring a variable "evenrun" with a value that is a tuple consisting of
    Code:
    ([a literal zero],
     [the result of the int() function with the argument "user_num"],
     [a literal two])
    So conceptually if the user inputs, say, "5", evenrun will be "(0, 5, 2)" (...or not, there are other problems below). Which is probably not what you want.

    But look at line 2. You're declaring evenrun 10 times, because range() returns an generator of (0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20), each of which get assigned to your iteration variable "i" in turn. But then you never reference the variable "i" again.

    It looks like you're trying to build a list based on integers in steps of 2 from zero, stopping at whatever number the user entered. So you need to build a list, not reassign a tuple 20 times. And speaking of that number 20... why is it there? Its not the number the user entered... which means your loop is missing the point.
    python Code:
    def evennum(i):
        for z in range(0, i, 2):
            # stuff involving each successive value of z...

    is more in the right direction. But remember the part about needing to build a list. Go read the Python docs about lists, or even cooler, about list comprehensions -- and you will find your solution.

    Comments on this post

    • Alf4JC agrees : Very Helpful
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    7
    Rep Power
    0

    Thanks


    wow thanks I hadn't realized I was so far off thanks for the help I will read what you suggested and try it again I can see what you are saying about not referencing i

    The 20 is there lol cuz I thought I needed to go that far but in reality I only need to go as far as the number that gets input.
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Originally Posted by Alf4JC
    The 20 is there lol cuz I thought I needed to go that far but in reality I only need to go as far as the number that gets input.
    So 20 is the biggest integer you know? What kind of school is this?
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Location
    Usually Japan when not on contract
    Posts
    240
    Rep Power
    12
    Originally Posted by Alf4JC
    wow thanks I hadn't realized I was so far off...
    Not too far off, really, just the whole "why am I labeling this and what has already been labeled" thing isn't yet clear in your mind. (Read "label" as "variable name" in the previous sentence.) You gave it a decent shot, and that's a lot more than most newcomers with homework problems do. I don't mind explaining things a bit to someone who is trying.

    Just keep thinking and paying attention. Truly fascinating stuff isn't too far away.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    7
    Rep Power
    0
    Originally Posted by zxq9
    Not too far off, really, just the whole "why am I labeling this and what has already been labeled" thing isn't yet clear in your mind. (Read "label" as "variable name" in the previous sentence.)
    Yes for some reason the labeling is throwing me off I did a far more complicated program for my mid term it was an ATM machine using for loop and if.

    I totally got that but this for some reason is confusing me. I will read everything ur suggesting need to turn this in tomorrow
    THANK YOU!!!! again for your time.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    7
    Rep Power
    0
    Originally Posted by Jacques1
    So 20 is the biggest integer you know? What kind of school is this?
    LOL no I just don't know what I am doing yet hahahah
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Location
    Usually Japan when not on contract
    Posts
    240
    Rep Power
    12
    > 20 is a huge engineering challenge. Anything greater than fingers + toes is > 20. Undermines the entire decision to go with a decimal system to begin with. So he was just identifying the numeric threshold to Danger and setting it as a safe limitation. Probably a selfless attempt to keep the world on an even kilter, a place where children could be safe and free from the hazards of sums over 20.

    [Yeah! I did it! I achieved a "reduction ad about-keeping-children-safe" -- an advanced form of the "reduction ad about-public-safety", itself an advanced form of "this rule is for your own good" -- from "> 20"! I really should ditch this "work" thing and become a populist politician...]

IMN logo majestic logo threadwatch logo seochat tools logo