-1

Can anyone explain to me what's wrong in this code:

def greater(a,b):
    if a > b:
        return a
    return b

num1, num2 = int(input("enter two number : ").split(","))
print(f"bigger is :  {greater(num1,num2)}")


TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
  • 2
    `split()` returns a list. You are passing that list to `int()`. – Mark May 17 '20 at 07:52
  • Does this answer your question? [Get a list of numbers as input from the user](https://stackoverflow.com/questions/4663306/get-a-list-of-numbers-as-input-from-the-user) – Tomerikoo May 17 '20 at 07:55

2 Answers2

1

You have

  • input("enter two number : ") that values let's say 1,2
  • input("...").split(",") is now ['1', '2'] so a list of 2 string

  • int(['1', '2']) << you can't do that


You need to map each value to int

num1, num2 = map(int,input("enter two number : ").split(","))

Or do

values = input("enter two number : ").split(",")
num1, num2 = int(values[0]), int(values[1])
azro
  • 53,056
  • 7
  • 34
  • 70
  • @DavidBuck oh thanks, didn't even try I thought i already get an error of unpacking from map, thanks – azro May 17 '20 at 07:56
  • There is a `)` missing after first `int` function in second line of second method. – rnso May 17 '20 at 08:21
0

You can also use following one liner:

print("Bigger is", max([int(x) for x in input("Enter 2 numbers separated by comma: ").split(",")]))

No need for your greater function.

Understanding above code yourself may be educative.

rnso
  • 23,686
  • 25
  • 112
  • 234