Sunday, May 24, 2020

While Loops and If Else Statements

In this lab, we were tasked with selecting and printing an item from a list, debugging a pre-written code for a dice game, using a while loop to create a random list of 20 numbers, selecting an unlucky number and printing how many times it showed up in the list using if else statements, and finally removing that number from the list using another while loop.  The results of my experience are shown below.



Part 1:

Create string variable consisting of full name
Split the name using a space as identifier
Print last name in list


Part 2:

Run the existing code to see what errors existing

Debug error, run again

Debug error, run again

Print successful results


Part 3:

Create empty list

Assign counter variable

While loop
     Generate random integer
     Append to list
     If statement to set list length to 20
          Break loop

Print list


Part 4:

Assign variable to unlucky number
Count method to see how many times number shows up in list

If statement to print if the number does not show up in the list
     Else statement to print if the number does show up, how many times it needs to be removed

While loop if z in lucky list
     Remove method to remove until the number no longer shows up in list

Print list


A few of the important things I learned from this lab are:

1 Keep it simple… If the code does not require additional steps, don’t add additional steps.

2 Very important to give a while loop the x+=1 command.

3 I was able to complete this task with a for loop before I was able to complete it with a while loop. And while I see the reasons for not using a for loop in general, I had a lot of trouble getting the while loop to work. I kept sticking with it and now it all seems rather simple. It did not feel that simple when I kept running into issues though. It makes for a very rewarding conclusion once it finally works.

4 I think one of the most important lessons for me is to not attempt too much code at once. Break the code down into subsequent parts and get it working as you go. I played around with having the 4th part of this assignment running before the 3rd part was completely successful. It didn’t necessarily hurt me to try, but I think you run the risk of information overload when trying to do too much at once.

No comments:

Post a Comment