`if`

, not `elif`

.Python

if max<a[i]: max=a[i] if min>a[i]: min=a[i]

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.

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.

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]

Permalink

Share this answer

Comments

CPallini
12-Dec-18 17:22pm

5.

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])

Permalink

Share this answer

Comments

Richard MacCutchan
12-Dec-18 17:51pm

Not lazy but clever.

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

What if all values are between 10 and 100 ?

you need to initialize

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

Share this answer

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

CodeProject,
20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8
+1 (416) 849-8900