Flowchart for Creation of the Module 2 Script
Step 3: Create a loop that adds 20 random numbers between
zero (0) and ten (10) to a list
The first step was to import the random module because the instructions specifically asked for a random number. **Like rolling dice.
Assign the lucky list to be empty so that it can be filled
in later.
Next is the while loop-
Set counter as indicated, start at zero,
We used the append function because that would instruct us to add the numbers to the empty luckyList.
We set the while i < 20 because this is how
many iterations we want until the loop equals false and stops
Then we used the append method to add the random integers, from ranges 0-10, to the empty list.
We really did not need to, but the instructions asked us to,
so we included an if and break statement that would prevent the list from going
further than 20 iterations.
Then we hit print luckyList so that we could print out a
list as requested.
***Using a for loop would have been a better, more
concise option.
Step 4: Create a loop that removes an unlucky number from
the list generated in step 3.
For this step, I began by copying luckyList, generated in
step 3, into the cell.
Then an integer is assigned to the variable x.
Created an if statement where if it was not equal to x, it
would say that it was not in the loop. However, if it were, the else statement
could invoke the count method to count how many times that integer occurred in
the list
Then, creating a print statement that mentioned how many
times a specific number was to be removed was rather confusing, and I had to
do some external research. I first had to write out the statement with parentheses
and variables within, for example, (x) and (count). I had to write a count
assignment statement just like I did for x.
Finally, the creation of the while loop using the remove method was used to remove the unlucky variable x from the previously generated list. I struggled with the creation of this loop because I did not write the remove method on the same line that mentioned the lucky list. Therefore, every time I tried to print the while loop, an error would appear saying that x did not appear in the list.
Finally, the updated luckyList with no variable x was
printed.
No comments:
Post a Comment