Tuesday, May 31, 2016

Python Fundamentals Part II

   This screenshot shows the results of a code that performs a dice rolling game based on the length of the players' name and creates a random list of 20 integers between 0 and 10. A conditional statement with a while loop is created to target a variable (the number 5 in this case) and remove it from the list for however many times it appears.

   How I created the code:

1.       I created a variable for the unlucky number then used a count method based on how many times the variable was in the list.
2.       I checked to see if the count variable was less than or equal to zero, if it was it would print “The number 5 is not in the list”
3.       If the variable was found in the list, a second conditional statement would state that the variable appeared and that its count would be deleted for however many times it appeared. Ex) the number 5 will be deleted 4 times.

4.       Next, a while loop was created in the second conditional statement that if the count of the variable was greater than zero, it would remove the variable count by 1 until the variable was completely removed from the list.

No comments:

Post a Comment