14,241,320 members
Rate this:
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:

## 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]
CPallini 12-Dec-18 17:22pm

5.
Rate this:

## 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])
Richard MacCutchan 12-Dec-18 17:51pm

Not lazy but clever.
Rate this:

## 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.