hangman(secret_word)内のguesses_remainingをget_guessed_word(secret_word, letters_guessed)でも続けて使える?共有するやり方を教えてください。語彙力がなくてごめんなさい。
python
17# Problem Set 2, hangman.py 2# Name: 3# Collaborators:なし 4# Time spent:4時間 5 6# Hangman Game 7# ----------------------------------- 8# Helper code 9# You don't need to understand this helper code, 10# but you will have to know how to use the functions 11# (so be sure to read the docstrings!) 12import random 13import string 14 15WORDLIST_FILENAME = "words.txt" 16 17 18 19def load_words(): 20 """ 21 Returns a list of valid words. Words are strings of lowercase letters. 22 23 Depending on the size of the word list, this function may 24 take a while to finish. 25 """ 26 print("Loading word list from file...") 27 # inFile: file 28 inFile = open(WORDLIST_FILENAME, 'r') 29 # line: string 30 line = inFile.readline() 31 # wordlist: list of strings 32 wordlist = line.split() 33 print(" ", len(wordlist), "words loaded.") 34 return wordlist 35 36 37 38def choose_word(wordlist): 39 """ 40 wordlist (list): list of words (strings) 41 42 Returns a word from wordlist at random 43 """ 44 return random.choice(wordlist) 45 46# end of helper code 47 48# ----------------------------------- 49 50# Load the list of words into the variable wordlist 51# so that it can be accessed from anywhere in the program 52wordlist = load_words() 53 54 55def is_word_guessed(secret_word, letters_guessed): 56 ''' 57 secret_word: string, the word the user is guessing; assumes all letters are 58 lowercase 59 letters_guessed: list (of letters), which letters have been guessed so far; 60 assumes that all letters are lowercase 61 returns: boolean, True if all the letters of secret_word are in letters_guessed; 62 False otherwise 63 ''' 64 i=0 65 for char_1 in secret_word: 66 i+=1 67 if char_1 not in letters_guessed: 68 return False 69 break 70 elif i==len(secret_word): 71 return True 72 73 74 75 76 77def get_guessed_word(secret_word, letters_guessed): 78 ''' 79 secret_word: string, the word the user is guessing 80 letters_guessed: list (of letters), which letters have been guessed so far 81 returns: string, comprised of letters, underscores (_), and spaces that represents 82 which letters in secret_word have been guessed so far. 83 ''' 84 listed_secret_word=list(secret_word) 85 for i in range(len(listed_secret_word)): 86 character_1=listed_secret_word[i] 87 if character_1 in letters_guessed: 88 continue 89 else: 90 listed_secret_word[i]='_ ' 91 if s in listed_secret_word: 92 return 'Good guess:'+''.join(listed_secret_word) 93 else: 94 guesses_remaining+=1 95 return 'Oops! That letter is not in my word:'+''.join(listed_secret_word) 96 97 98def get_available_letters(letters_guessed): 99 ''' 100 letters_guessed: list (of letters), which letters have been guessed so far 101 returns: string (of letters), comprised of letters that represents which letters have not 102 yet been guessed. 103 ''' 104 xyz=["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"] 105 for i in range(len(letters_guessed)): 106 character_2=letters_guessed[i] 107 xyz.remove(character_2) 108 return str(''.join(xyz)) 109 110 111 112 113def hangman(secret_word): 114 ''' 115 secret_word: string, the secret word to guess. 116 117 Starts up an interactive game of Hangman. 118 119 * At the start of the game, let the user know how many 120 letters the secret_word contains and how many guesses s/he starts with. 121 122 123 * The user should start with 6 guesses 124 125 * Before each round, you should display to the user how many guesses 126 s/he has left and the letters that the user has not yet guessed. 127 128 * Ask the user to supply one guess per round. Remember to make 129 sure that the user puts in a letter! 130 131 * The user should receive feedback immediately after each guess 132 about whether their guess appears in the computer's word. 133 134 * After each guess, you should display to the user the 135 partially guessed word so far. 136 137 Follows the other limitations detailed in the problem write-up. 138 ''' 139 140 guesses_remaining=0 141 print('Welcome to the game Hangman!' ) 142 print('I am thinking of a word that is '+ str(len(secret_word)) + ' letters long.' ) 143 while(guesses_remaining!=6): 144 print('-------------') 145 print('You have',str(6-guesses_remaining),'guesses left.') 146 print('Available letters:'+get_available_letters(letters_guessed)) 147 s=input('Please guess a letter:') 148 letters_guessed.append(s) 149 print(get_guessed_word(secret_word, letters_guessed)) 150 151if __name__ == "__main__": 152 # pass 153 154 # To test part 2, comment out the pass line above and 155 # uncomment the following two lines. 156 secret_word = choose_word(wordlist) 157 guesses_remaining=0 158 letters_guessed=[] 159 s='' 160 hangman(secret_word)
正直にいって、あなたの疑問点が分かりません。https://teratail.com/help/question-tipsを参考にして、可能な限り詳細、具体的に説明くださると回答得られるかもしれません。
回答1件
あなたの回答
tips
プレビュー