CSE 1010 Lecture 10: CSE1010 - Lecture 10 - More Loops

37 views2 pages
Verified Note

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.

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents

Related Questions