0

This is the code I wrote

try:
    def max_of_n(*args):
        max = 1
        for x in args:
            if x > max:
                max = x
        return max
    print(max_of_n(int(x) for x in input("Enter the numbers whose max you want to find").split(","))

except:
    print("You have entered wrong input")

And for above code I am getting below error

Traceback (most recent call last):
  File "C:/Users/Akshay/PycharmProjects/CodeClubProjectsAboutMe/Trial.py", line 8, in <module>
    max_of_n(int(x) for x in input("Enter numbers").split(","))
  File "C:/Users/Akshay/PycharmProjects/CodeClubProjectsAboutMe/Trial.py", line 4, in max_of_n
    if x > max:
TypeError: '>' not supported between instances of 'generator' and 'int'

Can you please help me here ? THE MAIN PROBLEM IS HOW PASS COMMA SEPARATED NUMBER INPUTS TO MY FUNCTION ?

  • Are you done editing yet? – Stephen Rauch Mar 26 '18 at 04:01
  • The error is explained in the error message, you are passing a generator object to your `max_of_n` function, i.e. you are passing `(int(x) for x in input("Enter the numbers whose max you want to find"))` But then your function takes the first argument and ties to compare it to an `int` object, but you cannot compare `int` objects and generator objects... – juanpa.arrivillaga Mar 26 '18 at 04:02
  • Also, this is a really bad way to do error handling. Why is your *function definition* in the try block? Anyway, your function would actually work if you didn't use `*args` and just use d a normal positional argument. – juanpa.arrivillaga Mar 26 '18 at 04:03
  • The bit you seem to be asking for is just adding `.split(',')` to the input string. That will give you each of the comma-separated parts of the string, instead of each of the characters of the string. You still have bugs in your code that you need to fix, but that should get you past this hurdle. – abarnert Mar 26 '18 at 04:05
  • Thanx juanpa,I want the function to take arbitary number of inputs hence I have using *args. Could you help to resolve the generator error ? – Akshay Ugale Mar 26 '18 at 04:07
  • the simplest way would be to unpack the iterable into the function call. sometimes called "splatting". See the linked duplicates – juanpa.arrivillaga Mar 26 '18 at 04:10

0 Answers0