Reputation:
I'm a beginner at programming, and I'm trying to create a program that generates 1000 random numbers, and then prints out the highest value of the numbers in the list of numbers (I am aware that my program only outputs 10 numbers, it is because I'm trying to debug the program). I already have a program, but it's not working. The problem is, the program will output these seemingly random numbers, sometimes its the largest number, and other times its a list of more than one number. Help would be greatly appreciated.
import random
num_list=[]
largest=[]
maximum=-1
while len(num_list)<10:
numbers=random.randrange(0,11)
numbers=int(float(numbers))
num_list.append(numbers)
if len(num_list)==10:
print num_list
for num in num_list:
if num>maximum:
maximum=num
largest.append(num)
print largest
Upvotes: 0
Views: 6875
Reputation: 2040
A more efficient way of doing this:
numList = []
for i in range(10):
numList.append(random.randrange(0,11))
maxNum = -1
for i in numList:
if i > maxNum:
maxNum = i
print(maxNum)
Upvotes: 2
Reputation: 598
reduce(lambda x, y: x if x > y else y, [random.randrange(0,11) for i in range(10)])
Upvotes: 0
Reputation: 304483
This makes no sense
maximum=num
maximum.append(num)
perhaps you mean this
maximum=num
largest.append(num)
Upvotes: 1