1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

python fuction

Discussion in 'Other Languages' started by koolade, Oct 8, 2011.

  1. koolade

    koolade Newbie

    Joined:
    Aug 13, 2010
    Messages:
    24
    Likes Received:
    0
    hey guys i need help to write a very short bit of code for my class. I have racked my brain for a couple hours and simply cant make anything work. its in python and i have to right a program that takes input from a user for 2 number and return the greater of the two i have to use a function called max to do this and no matter how i try to right it it will not work without errors its prolly something simple that im missing
     
  2. charlie3

    charlie3 Senior Member

    Joined:
    Oct 4, 2009
    Messages:
    1,046
    Likes Received:
    468
    Location:
    U of A
    It should be something like this:

    a = input('Enter Number1: ')
    b = input('Enter Number2: ')
    if a > b:
    return a
    else:
    return b

    I'm actually in my first Python class this semester at the University of Arizona so don't think I'm some awesome programmer for years, but I think that should be about right. I'm still trying to wrap my head around functions, but hopefully that should help you a little bit :D

    What school do you go to? I've been around this forum for a while and never seen anyone ask about Python. Good luck!
     
    • Thanks Thanks x 1
  3. koolade

    koolade Newbie

    Joined:
    Aug 13, 2010
    Messages:
    24
    Likes Received:
    0
    i could do that as well and thats only way im making it work, but i need it to be done as a function and i cant make it work such as i need to make that be the function
     
  4. Silverion

    Silverion Junior Member

    Joined:
    Dec 20, 2009
    Messages:
    180
    Likes Received:
    28
    Even it can rack my brain.I don't have knowledge of python language.:(
     
  5. koolade

    koolade Newbie

    Joined:
    Aug 13, 2010
    Messages:
    24
    Likes Received:
    0
    oh i didnt even see that i go to edison college in fort myers florida. ah as far as the program i get errors no matter how i right or what i do when trying to call the function or define the function
     
  6. scriptomania

    scriptomania Junior Member

    Joined:
    Dec 28, 2010
    Messages:
    127
    Likes Received:
    249
    Occupation:
    A full time pirate at sea
    Location:
    The European capital of politics
    So... this didnt work?

    Code:
    a = raw_input("First number: ")
    b = raw_input("Second number: ")
    c = max(int(a), int(b))
    print c
    And as a function:

    Code:
    def greater_of_two(a, b):
        try:
            return max(int(a), int(b))
        except Exception:
            return "Error"
    
    a = raw_input("First: ")
    b = raw_input("Second: ")
    print greater_of_two(a, b)
    
     
    • Thanks Thanks x 1
  7. koolade

    koolade Newbie

    Joined:
    Aug 13, 2010
    Messages:
    24
    Likes Received:
    0
    that works i just need the finction called max and it be a little more simplfied were not that advanced yet
     
  8. William Rufino

    William Rufino Newbie

    Joined:
    Aug 3, 2011
    Messages:
    32
    Likes Received:
    2
    the is the simples you'll get man

    Code:
    a = raw_input("First number: ")
    b = raw_input("Second number: ")
    c = max(int(a), int(b))
    print c
    
     
    • Thanks Thanks x 1
  9. koolade

    koolade Newbie

    Joined:
    Aug 13, 2010
    Messages:
    24
    Likes Received:
    0
    it needs to be a function called max
     
  10. koolade

    koolade Newbie

    Joined:
    Aug 13, 2010
    Messages:
    24
    Likes Received:
    0
    Design a function namesd max that accepts two integer values as arguments and returns the value that is the greater of the two. For example, if 7 and 12 are passed as argurments to the function,, the fuction should returm 12. use the function in a prgram that promts the user to enter two values. The program should display the value that is greater of the two.

    that is the question the one answer with try works but that is beyond our comprehension as i have not seen that in my book.
     
  11. scriptomania

    scriptomania Junior Member

    Joined:
    Dec 28, 2010
    Messages:
    127
    Likes Received:
    249
    Occupation:
    A full time pirate at sea
    Location:
    The European capital of politics
    Code:
    def my_max(a,b):
        try:
            if int(a) > int(b):
                return a
            elif int(a) < int(b):
                return b
            else:
                return 0
        except Exception:
            return "error"
    
    a = raw_input("First: ")
    b = raw_input("Second: ")
    print my_max(a,b)
    
    There you go...
     
    • Thanks Thanks x 1
  12. koolade

    koolade Newbie

    Joined:
    Aug 13, 2010
    Messages:
    24
    Likes Received:
    0
    so you have to use try: and exception? cause like is said i can find that in my book
     
  13. scriptomania

    scriptomania Junior Member

    Joined:
    Dec 28, 2010
    Messages:
    127
    Likes Received:
    249
    Occupation:
    A full time pirate at sea
    Location:
    The European capital of politics
    What kinda book are you using?
    To answer your question, no you dont have to use exception handling, but its good practice to do so. Helps you catch some errors (what would happen if the user does not enter any numbers? What happens if the user leaves out a number all together?).

    Also, regarding the book, its best to check out the official python documentation or consult Google. These two sources will pretty much cover everything.

    Cheers
     
  14. koolade

    koolade Newbie

    Joined:
    Aug 13, 2010
    Messages:
    24
    Likes Received:
    0
    the book im using isnt even a python book but we have a pdf file that our teacher gave us to use that it also does not over it. I just changed it around some made the varibles more readable for everyone and added comments and turned it in Thanks for all your help hopefully i can start returning the favor soon!!!
     
  15. scriptomania

    scriptomania Junior Member

    Joined:
    Dec 28, 2010
    Messages:
    127
    Likes Received:
    249
    Occupation:
    A full time pirate at sea
    Location:
    The European capital of politics
    Ah, no worries. Good luck with your studies, feel free to drop me a PM if you have more questions.

    Cheers