- Python
- VS Code
When using VS Code, the user can click the Python interpreter button. This will open the terminal for the user, and a prompt will appear asking the user to enter the secure 4-digit code. Once the user has entered a secure code, the computer will then try to guess the code using brute force. A while loop will run until the computer guesses correctly. When the guess is correct, the loop breaks and the terminal will display what the code is and how many guesses it took.
As this was part of an introductory workshop, the instructor walked all attendees through each line with a brief overview (being mindful of time constraints). The first thing we did was import any necessary elements (e.g., os and random).
Then, we had to define the function to be called. We gave it a name to output and define the number of guesses and secure code (converting the user's input into an integer to get the 4-digit code). Then, we established a while loop to randomly generate four numbers to use as guesses and kept a tracker of the number of guesses (adding 1 to each additional guess).
Then we added an if statement: if guess is equal to secure code, print the code and number of guesses and break the loop. After the function was defined, we called the function.
The learnings for this project was difficult at first, as the explanations were brief to allow enough time to cover all the material. As I didn't have much experience with Python, I only had a vague grasp of each part; although, I had a clearer understanding when parts overlapped with my JS knowledge (e.g., defining and calling functions).
Since partipating in this workshop, I have completed a Python Developer certificate through Sololearn, which means I have a more thorough understanding of each line of code in the project. For example, a while loop is the better option (than for loop) because we don't know how many attempts it would take to crack the code. Also, breaking the code will stop an infinite loop from occurring.
Here are the improvements I would make as my skills grow:
- Each guess can only be made once. Currently, I believe the computer can reguess the same incorrect code. By removing the system's ability to reguess wrong numbers, finding the correct secure code would be faster.
- Adding UI. I think it would be a fun challenge to add a visual for the user rather than using the terminal in VS Code.
VS Code terminal prompting the user to enter a code.
VS Code terminal after code has been guessed correctly.