Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
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

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