2 תשובות
# the functions we'll use:

def welcome_screen():
""" prints the welcome screen, returns nothing """

hangman_ascii_art=r"""welcome to the game hangman
_ _
| | | |
| |__| | __ _ _ __ __ _ _ __ ___ __ _ _ __
| __ |/ _' | '_ \ / _' | '_ ' _ \ / _' | '_ \
| | | | (_| | | | | (_| | | | | | | (_| | | | |
|_| |_|\__,_|_| |_|\__, |_| |_| |_|\__,_|_| |_|
__/ |
|___/
"""
max_tries = 6
print(hangman_ascii_art+"\n"+str(max_tries))
return

def choose_word(file_path, index):
""" finds the number of different words in the file and the word at the
chosen index in the file.
:param file_path: the path to the file containig the words
:param index: the index of the secret word
:type file_path: str
:type index: int
:return: a tuple: (number of unique words, secret word)
:rtype: tuple: (int, str)
"""
with open(file_path, 'r') as input_file:
input_file_content = input_file.read().replace('\n', '')
# because i don't want '\n'
words_list = input_file_content.split(' ')
secret_word_pos = index % len(words_list)
# because if the number is more than the number of words we continue
# counting in a circular form so we only care about the remainder
secret_word = words_list[secret_word_pos - 1]
# becuase we're given the pos, not the index and indexes start from 0
return secret_word

def print_hangman(num_of_tries):
""" the function prints one of the 7 states if the hangman according to
the num of fails the player has made.
:param num_of_tries: the number of fails the player has made
:type num_of_tries: int
:return: none
:rtype: nonetype
"""
hangman_photos = dict()
hangman_photos['state 0'] = 'x-------x'
hangman_photos['state 1'] = """
x-------x
|
|
|
|
| """
hangman_photos['state 2'] = """
x-------x
| |
| 0
|
|
|
"""
hangman_photos['state 3'] = """
x-------x
| |
| 0
| |
|
|
"""
hangman_photos['state 4'] = r"""
x-------x
| |
| 0
| /| |
|
"""
hangman_photos['state 5'] = r"""
x-------x
| |
| 0
| /| | /
|
"""
hangman_photos['state 6'] = r"""
x-------x
| |
| 0
| /| | / |
"""
print(hangman_photos['state {}'.format(num_of_tries)])

def check_win(secret_word, old_letters_guessed):
""" checks if the player has won (all the letters in the secret_word
have been guessed).
:param secret_word: the word the player needs to guess
:old_letters_guessed: the letters the player has already guessed
:type secret_word: str
:type od_letters_guessed: list
:return: he won - true, else - false
:rtype: bool
"""
for i in secret_word:
if (i not in old_letters_guessed):
return false
return true

def show_hidden_word(secret_word, old_letters_guessed):
""" the function represents the secret word by the letters guessed in
their right places, and the rest is replaced with underscores.
:param secret_word: the word to be represented
:param old_letters_guessed: all the letters the player guessed already
:type secret_word: str
:type old_letters_guessed: list
:return: the represantation of the secret word
:rtype: str
"""
represented_secret_word = ''
for i in secret_word:
if (i in old_letters_guessed):
represented_secret_word += i
else:
represented_secret_word += '_'
represented_secret_word += ' ' # seperated with spaces for convenience
return represented_secret_word


def check_valid_input(letter_guessed, old_letters_guessed):
"""
the function gets the letter guessed by the player and returns wether
the guess isn't valid (the guess' length is more than one character
or it contains something which isn't an english letter or its has been
already guessed) or it is (one character, from the english alphabet, not in
the list old_letters_guessed).
:param letter_guessed: the player's guess
:param old_letters guessed: a list containing all of the previously guessed
letters.
:letter_guessed type: str
:old_letters_guessed type: list (of lowercase letters)
:return: true if the character is valid, false if it isn't
:rtype: bool
"""
if (len(letter_guessed) == 1) and (letter_guessed.isalpha()) and (letter_guessed not in old_letters_guessed):
# lowercase b/c the letters in the list are all converted to lowercase
indicator = true
else:
indicator = false
return indicator

def try_update_letter_guessed(letter_guessed, old_letters_guessed):

""" the function gets a character: if it's valid it'll add it to the list
old_letters_guessed. else: the function wiil print x and below it the list
old_letters_guessed as a string of lowercase letters, sorted alphabetically
and seperated by arrows (->). at last, the function will return false.
:param letter_guessed: the player's guess
:param old_letters guessed: a list containing all of the previously guessed
letters.
:letter_guessed type: str
:old_letters_guessed type: list (of lowercase letters)
:return: true if the character is valid, false if it isn't
:rtype: bool
"""
indicator = check_valid_input(letter_guessed, old_letters_guessed)
if indicator:
old_letters_guessed.append(letter_guessed)
else:
print('x')
print(' -> '.join(sorted(old_letters_guessed)))
return indicator

# the main program:

def main():
max_tries = 6
welcome_screen()
file_path = input('enter a file path: ')
secret_word_pos = int(input('enter index: '))
secret_word = choose_word(file_path, secret_word_pos)
print('let\'s start!')
old_letters_guessed = []
num_of_tries = 0
print_hangman(num_of_tries)
print(show_hidden_word(secret_word, old_letters_guessed))
while (check_win(secret_word, old_letters_guessed) == false) and (num_of_tries < max_tries):
# aka: while game isn't over
letter_guessed = input('guess a letter: ').lower()
# because the secret word is built out of lower case letters
indicator = try_update_letter_guessed(letter_guessed, old_letters_guessed)
if indicator and (letter_guessed not in secret_word):
print(':(')
num_of_tries += 1
print_hangman(num_of_tries)
if indicator:
print(show_hidden_word(secret_word, old_letters_guessed))
if check_win(secret_word, old_letters_guessed):
print('win')
else:
print('lose')


if __name__ == "__main__":
main()
שואל השאלה:
וואו תודה רבהה