15,745,335 members
1.00/5 (1 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

## Solution 1

The second test below should just be `if`, not `elif`.
Python
```if max<a[i]:
max=a[i]
if min>a[i]:
min=a[i]```

CPallini 12-Dec-18 17:22pm
5.

## Solution 2

The lazy people solution
Python
```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.

## Solution 3

A problem in your code is that you are guessing that you will values both over and under zero.
Python
```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.