dimanche 25 septembre 2016

Random List, Find Max

I need to generate a random list of 100 numbers, then go through that list until I find the max number using a for loop, but it only runs through once (because num is outside the loop), but when I put num in the loop nothing happens

import random

def main():

    largest = None

    num = []
    num.append(random.randint(1,500))

    count = 0

    for n in num:


        count = count + 1

        print 'The largest known number is,', largest

        if n > largest:

            largest = n
            print 'Next largest number known is,',largest,'\n'
            print 'It took', count,'trys to find the largest number'


main()




Aucun commentaire:

Enregistrer un commentaire