python fuction

koolade

Newbie
Joined
Aug 13, 2010
Messages
26
Reaction score
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
 
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!
 
a = input('Enter Number1: ')
b = input('Enter Number2: ')
if a > b:
return a
else:
return b

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
 
Even it can rack my brain.I don't have knowledge of python language.:(
 
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
 
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)
 
that works i just need the finction called max and it be a little more simplfied were not that advanced yet
 
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
 
it needs to be a function called max
 
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.
 
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...
 
so you have to use try: and exception? cause like is said i can find that in my book
 
so you have to use try: and exception? cause like is said i can find that in my book

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
 
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!!!
 
Ah, no worries. Good luck with your studies, feel free to drop me a PM if you have more questions.

Cheers
 
Back
Top