Featured Post

Welcome to My Blog

Monday, May 27, 2024

Module 2: Python Fundamentals

    In this week's assignment, I learned about different types of objects and some functions that can be applied to these objects. To illustrate that, I created a string of my full name. Next, I converted the string into a list with each name as a single item in the list. Lastly, I used indexing to return only my last name. 

    In addition, I practiced debugging skills by correcting the errors in the code for a dice game. Through this exercise, I was able to get acquainted with the steps to take when my code is giving me errors. Using the clues given in the error codes, I was able to look up solutions in the textbook and online, so that the dice would run properly.These were my results once the code was updated!

Lastly, I had to apply what I had learned about the different types of objects, various functions in python, debugging, with/for loops, and if/else statements to generate a list of randomly generated number and remove an unlucky integer. Initially, I had difficulty with my syntax, so the code that I created was giving me syntax errors. Once I resolved those issues, I was able to generate the necessary output.

I started by importing the random module to help with the random number generation that would come later. Then, I created a list with no items called luckyList. Next, I used a while statement to indicate that while luckList contained less than 20 items (using the len function) another random number between 0-10 should be appended to the list. Then, I used an if statement to indicate that if the luckyList contained 20 items, then the luckyList should be printed

To remove the unlucky integer, I started by creating an item called unlucky which represented my unlucky integer, 7. Then, I used an if/else statement to indicate if the unlucky integer was present in the luckyList and I indicated what steps should follow if it was present or if it was absent from the list. If it was absent, the luckyList would be printed again as is. If the unlucky integer was present, the following statement would be printed:

“<unlucky integer (7)> will be removed from the list <count of the unlucky integer in luckyList> times.”
    Note: I used the str() function to convert the integers (i.e. count result) into a string

Last, I used a while statement to dictate that the unlucky integer be removed from the luckyList while the count of the unlucky integer was greater than 0. Finally, the final lucky list was printed again without the unlucky integer.




No comments:

Post a Comment