User Tools

Site Tools


lists

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
lists [2023/10/27 02:27] appledoglists [2023/10/27 02:55] (current) appledog
Line 106: Line 106:
     num = 5     num = 5
     list = []     list = []
-     + 
-    for x in range(num):+    while len(list< num:
         f = random_fruit()         f = random_fruit()
         list.append(f)         list.append(f)
Line 138: Line 138:
     main()     main()
 </Code> </Code>
 +
 +== Homework
 +Now you can build a function that pairs students into groups. There basic idea is the same, but, make sure that the same student is not added to a group using the check "if name not in list:". This will allow you to make a group of students which do not contain the same student. If you wish to create a number of groups at the same time, you can use the pop, del, or remove commands. For example;
 +
 +<Code:Python>
 +    n = random.choice(len(students))
 +    s = students.pop(n)
 +    group.append(s)
 +</Code>
 +
 +The code above moves the student out of the list called "students" and adds them to the list called "group". The benefit of this is that since it removes the student from the pool of possible choices, you do not need to check for duplicates when randomly picking a student.
 +
 +== Next
 +Next, we can look at [[Lists II]], however, you might also want to look at [[Basics III]] to see how lists are used in action!
lists.1698373627.txt.gz · Last modified: 2023/10/27 02:27 by appledog

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki