+ 4
How to write a program that will print maximum of 3 numbers
13 Réponses
+ 5
Aliyu Dansale Yahya
Just fix one element as max_number, and then go through whole list and if you find any element bigger than max_number then change value of max_number to that element(thats what Aymane Boukrouh did, this technique is used in selection sort).
+ 5
Use max function
+ 3
I mean if i have a list of 3 numbers what can i do to print the max between them
+ 3
def max(l):
s = l[0]
for i in l:
if(i>s):
s=i
return s
l = [1, 5, 3]
print(max(l))
Output: 5
This function is already a builtin, so you can use it by default without having to define it.
+ 2
Thanks alots my friends but can i do it without function
+ 2
hafeezudeen did you just copy paste my answer ?
+ 2
Please don't copy-paste others' answers. You don't gain anything from it.
+ 1
The built in max() does the job.
max(1,2,3) or max([1,2,3]) will work.
0
I guess u will use max funtion.. I. E max([list])....not sure though coz am just learning
0
Aliyu Dansale Yahya umm, I believe I gave the solution ? Just instead of making it a function write it directly:
l = ...
s = 0
for...
Simply rewrite what I did, and instead of return you print it
0
Sort the list in descending order.
Print the first item on the list.
0
print(max(4,5,6))
print(max([4,5,6]))
- 3
def max(l):
s = l[0]
for i in l:
if(i>s):
s=i
return s
l = [1, 5, 3]
print(max(l))
Output: 5
This function is already a builtin, so you can use it by default without having to define it.