Day 6
Day 6 of Angela Yu's Python Pro bootcamp involved controlling a robot with Python functions and while loops. The goal of day 6's project was to help the robot escape a maze by using while loops and functions.
def right_turn():
turn_left()
turn_left()
turn_left()
while front_is_clear():
move()
turn_left()
while not at_goal():
if right_is_clear():
right_turn()
move()
elif front_is_clear():
move()
else:
turn_left()
Day 7
Day 7 of the bootcamp involved learning about while not loops and the not operator in if statements. The goal of this project was to build a hangman game that asks the player to enter letters before their lives run out. Players start out with 6 lives and if they get a letter wrong, it's decremented by 1 each time until it reaches zero and the game ends.
import random
import hangman_art
import hangman_words
#TODO-1: - Update the word list to use the 'word_list' from hangman_words.py
chosen_word = random.choice(hangman_words.word_list)
display = []
end_of_game = False
lives = 6
#TODO-3: - Import the logo from hangman_art.py and print it at the start of the game.
print(hangman_art.logo)
for letter in chosen_word:
display += "_"
print(display)
while not end_of_game:
guess = input("Guess a letter: ").lower()
word_length = len(chosen_word)
# Check guessed letter
for position in range(word_length):
letter = chosen_word[position]
if letter == guess:
display[position] = letter
if guess not in chosen_word:
lives -= 1
print(f"Number of lives: {lives}")
if lives == 0:
end_of_game = True
print("You lose!")
# Join all the elements in the list and turn it into a String.
print(f"{' '.join(display)}")
if "_" not in display:
end_of_game = True
print("You win!")
# TODO-2: - Import the stages from hangman_art.py
print(hangman_art.stages[lives])
Originally, the code students are given in this bootcamp involves all of the words contained in a list named word_list in hangman_words.py. However, I have modified the code by putting all of the words in a text file and have the program read all the words in the text file when the game starts.
print("Loading wordlist from file...")
file_open = open("wordlist_file.txt", 'r')
word_list = [line for line in file_open] # Used list comprehension instead of for loop to make code more Pythonic.
print(" ", len(word_list), " words found.")
I love that Python has simple syntax for reading into a file, like open() and 'r'. Edit: I have replaced the for loop with list comprehension as I have just discovered it is a way of writing a for loop in one line of code.
In C#, the above code would be written as:
int word_counter = 0;
Console.WriteLine("Loading wordlist from file...")
foreach (string line in System.IO.File.ReadLines("wordlist_file.txt"))
{
word_counter++;
Console.WriteLine(word_counter + " words found.");
The difference between the two is that Python adds the words into an empty list while C# uses a counter to count each line that is read. I did not do the Hangman game in C# because there isn't a .NET equivalent of random.choice() that I know of.
I will post about Day 8 by the end of this week.
Top comments (0)