CSE 1010 Lecture 10: CSE1010 - Lecture 10 - More Loops
CSE 1010 verified notes
10/12View all
Document Summary
Generate a list of random numbers, each in the range 0 to 10. Import random nums = [] #this generates an empty list n = 0 while n<10: #loop control variable num = random. randint(0,10) #generate a single random number and put it nums. append(num) #the we add the number to the list nums n = n+1 print (nums) #need to increment n since this is a while loop into the variable num. Output: list of ten random numbers, each in the range 0 to 10. Using a for loop: import random nums = [] for n in range(10): #n will be assigned each of the values from 0 to 9 num = random. randint(0,10) nums. append(num) #no need to increment when using a for loop print(nums) You get the same output as with the while loop. We will call this module numlist2 to use it later on.