-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Procedural Hangman Assignment #5
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
great effort @braidsencurls ! Just noticed there are some data structure/looping and best practices you'll need to familiarize yourself with.. don't worry, it'll come as you practice more... do check out google python style guide - https://github.com/google/styleguide/blob/gh-pages/pyguide.md
|
||
|
||
def _get_random_word(list_of_words): | ||
pass | ||
if len(list_of_words) != 0: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for sequences (strings, lists, tuples), use the fact that empty sequences are false, so if list_of_words
is preferable to if len(list_of_words)
|
||
|
||
def _get_random_word(list_of_words): | ||
pass | ||
if len(list_of_words) != 0: | ||
random_index = randint(0, len(list_of_words)-1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you may simply return random.choice(list_of_words)
... the choice() method will randomly pick from your list :)
can check out here for documentation - https://docs.python.org/3/library/random.html
if len(list_of_words) != 0: | ||
random_index = randint(0, len(list_of_words)-1) | ||
else: | ||
raise InvalidListOfWordsException() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's actually more intuitive to check for abnormalities, then raise the exception and continue with your logic instead of doing it the other way round ...
if not list_of_words:
raise InvalidListOfWordsException()
#your other logic here
|
||
|
||
def _mask_word(word): | ||
pass | ||
if word != '': |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
similarly, it's preferred to use if word:
|
||
|
||
def _mask_word(word): | ||
pass | ||
if word != '': | ||
letters_of_word = list(word) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you don't need to convert string to list .. in python, string is iterable as well.. meaning you can do this..
for letter in word:
print(letter)
|
||
|
||
def _uncover_word(answer_word, masked_word, character): | ||
pass | ||
|
||
|
||
if len(answer_word) == 0: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
prefer to be if not answer_word
for False check
raise InvalidWordException() | ||
else: | ||
answer_word = answer_word.lower() | ||
characters = list(answer_word) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same as above, you do not need to convert string to list to loop through it.. remember, string
is technically a "collection of characters" just like list
being "collection of items"
def guess_letter(game, letter): | ||
pass | ||
if game['remaining_misses'] == 0 or game['answer_word'].lower() == game['masked_word'].lower(): | ||
raise GameFinishedException | ||
if len(list(letter)) > 1: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if len(letter) > 1:
again, you don't need to convert to list ... everything a collection can do, string
should be able to do as well.. cause string
is a type of collection too ...
if letter.lower() in game['answer_word'].lower(): | ||
game['remaining_misses'] = int(remaining) | ||
else: | ||
game['remaining_misses'] = int(remaining) - 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can use shorthand like game['remaining_misses'] += 1
raise GameFinishedException | ||
if len(list(letter)) > 1: | ||
raise InvalidGuessedLetterException | ||
guesses = game['previous_guesses'] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you don't necessarily need to create variable for game[xxx]
so you can safe a few lines of code .. it actually does not save any memory / improve readability ....
for review