Click here to Skip to main content
14,241,320 members
Rate this:
Please Sign up or sign in to vote.
See more:
N Enter a natural number. Enter a random number (-100 and 100) in the list. Find max and min from them.

What I have tried:

a = [random.randint(-100,100) for i in range (10)]
max=0
min=0
print(a)
for i in range(len(a)):
if max<a[i]:
max=a[i]
elif min>a[i]:
min=a[i]
print(max,min)



but it doesn't answer i,e doesn't work.
Posted
Updated 12-Dec-18 14:58pm
Rate this:
Please Sign up or sign in to vote.

Solution 1

The second test below should just be if, not elif.
if max<a[i]:
    max=a[i]
if min>a[i]:
    min=a[i]
   
Comments
CPallini 12-Dec-18 17:22pm
   
5.
Rate this:
Please Sign up or sign in to vote.

Solution 2

The lazy people solution
import random
a = [random.randint(-100,100) for i in range (10)]
print(a)
a.sort()
print(a[-1],a[0])
   
Comments
Richard MacCutchan 12-Dec-18 17:51pm
   
Not lazy but clever.
Rate this:
Please Sign up or sign in to vote.

Solution 3

A problem in your code is that you are guessing that you will values both over and under zero.
max=0
min=0

What if all values are between 10 and 100 ?
you need to initialize min and max with the first random value.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100