+ 4

How to write a program that will print maximum of 3 numbers

27th Sep 2019, 5:57 PM
Aliyu Dansale Yahya
13 Respostas
+ 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).
27th Sep 2019, 6:26 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 5
Use max function
29th Sep 2019, 2:01 AM
Vijay(v-star🌟)
Vijay(v-star🌟) - avatar
+ 3
I mean if i have a list of 3 numbers what can i do to print the max between them
27th Sep 2019, 6:02 PM
Aliyu Dansale Yahya
+ 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.
27th Sep 2019, 6:09 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 2
Thanks alots my friends but can i do it without function
27th Sep 2019, 6:12 PM
Aliyu Dansale Yahya
+ 2
hafeezudeen did you just copy paste my answer ?
28th Sep 2019, 1:53 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 2
Please don't copy-paste others' answers. You don't gain anything from it.
29th Sep 2019, 5:00 AM
Sonic
Sonic - avatar
+ 1
The built in max() does the job. max(1,2,3) or max([1,2,3]) will work.
28th Sep 2019, 3:28 PM
Loïc Mahé
Loïc Mahé - avatar
0
I guess u will use max funtion.. I. E max([list])....not sure though coz am just learning
27th Sep 2019, 6:10 PM
Oluwakorede Serah
Oluwakorede Serah - avatar
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
27th Sep 2019, 6:13 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
0
Sort the list in descending order. Print the first item on the list.
27th Sep 2019, 6:15 PM
Philips Ekuma
Philips Ekuma - avatar
0
print(max(4,5,6)) print(max([4,5,6]))
27th Sep 2019, 7:12 PM
rodwynnejones
rodwynnejones - avatar
- 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.
28th Sep 2019, 1:50 PM
hafeezudeen
hafeezudeen - avatar