Why does max() produce wrong output in python?

numbers=input("Enter numbers separated by space")

Input is 4 5 29 54 4 0 -214 542 -64 1 -3 5 6 -6.

The output should be 542 but I'm getting the output as 6!

Convert them into integers and then apply function.

numbers=input("Enter numbers separated by space")
x=[int(i) for i in numbers.split()]



15. Floating Point Arithmetic: Issues and Limitations — Python 3.8.5 , Historically, the Python prompt and built-in repr() function would choose the one For more pleasant output, you may wish to use string formatting to produce a� Python Input and Output: Conclusion. In this tutorial, you learned about Python input and output, and how your Python program can communicate with the user. You should now be able to obtain data from the user with input(), and display results to the console with print().

input is in string. You need to convert it to ints.

numbers=input("Enter numbers separated by space")
x=map(int, numbers.split())


2. Built-in Functions — Python 3.3.7 documentation, Because dir() is supplied primarily as a convenience for use at an The optional sign may be '+' or '-'; a '+' sign has no effect on the value produced. If the prompt argument is present, it is written to standard output without a trailing newline. max(iterable, *[, key])�; max(arg1, arg2, *args[, key]). Yes , what I can see that, the problem is you are asking for a string . x=raw_input(“Enter strings “) And here you are comparing your character of string with an integer .

Others have explained whats going on but since Python has a repl, I'll use that to show you what python believes is happening.

>>> nums = input('insert space separated numbers ')
insert space separated numbers 1 2 3 4 5
>>> nums
'1 2 3 4 5'
>>> type(nums)
<class 'str'>
>>> nums.split(' ')
['1', '2', '3', '4', '5']
>>> type(nums.split(' '))
<class 'list'>

It all makes sense up to here. We read a string from user and when we split it, we get a list.

>>> type(nums.split(' ')[0])
<class 'str'>

This should still make sense but is what needs explaining. We split a string into a list of characters. In order to interpret that as an integer we need to call the int method on it.

>>> [int(x) for x in nums.split(' ')]
[1, 2, 3, 4, 5]

Putting it all together

>>> max([int(x) for x in nums.split(' ')])

To help explain more about above,

>>> '1' == 1
>>> '1' < 2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  TypeError: '<' not supported between instances of 'str' and 'int'

python for everybody, Once 'done' is entered, print out the largest and smallest of the numbers. If the user enters anything print ("Maximum is",largest) print ("Minimum is" This code produces correct output. What is wrong with it? Like; Quote� Python max() The Python max() function returns the largest item in an iterable. It can also be used to find the largest item between two or more parameters.

3. Functions — How to Think Like a Computer Scientist: Learning , In Python, the syntax for a function definition is: The output of this program is: First line. Second line. The extra space between the two lines is a result of the new_line() function Another built-in function that takes more than one argument is max. Functions may or may not take parameters and may or may not produce a� Home; Python Built-in Functions; Python max() function (Sponsors) Get started learning Python with DataCamp's free Intro to Python tutorial.Learn Data Science by completing interactive coding challenges and watching videos by expert instructors.

Program Examples, The print() function is then used to output all of the information. Python will automatically decide what type of data a variable should be, sometimes the capital city of England is London") else: #displays wrong if they have entered as the maximum value in the loop for x in range(0,int(age)): print("Your name is " + name). Python Examples Python Examples Python Compiler Python Exercises Python Quiz Python Certificate. The max() function, to return the highest value. Built-in Functions.

Python Basic: Exercises, Practice, Solution, Practice with solution of exercises on Python Basic: Examples on Twinkle, twinkle, little star, How I wonder what you are" Output : of comma-separated numbers from user and generate a list and a tuple with Sample function : abs() Write a Python function to find the maximum and minimum numbers� max() is an inbuilt function in Python programming language that returns the highest alphabetical character in a string. Output: s r My Personal Notes arrow_drop_up.

  • Strings are compared lexicographically. Convert the strings to ints.
  • But how is 6 greater than 542
  • Or use their integer form as the key in max
  • en.wikipedia.org/wiki/Lexicographical_order
  • @RakshithKrish The string "6" is greater than the string "542" because it starts with a greater character. It's the same reason that "fog" comes after "epicycle" in a dictionary, despite the later having more characters.