#Exercise 1
def GetAnswerWithUnderscores(answer,letters_guessed):
new_string=''
for ans in answer:
if ans in letters_guessed:
new_string+=ans
elif ans not in letters_guessed:
new_string+='_'
return new_string
answer = GetAnswerWithUnderscores('welcome', 'mel')
print('GetAnswerWithUnderscores #1: expected _el__me, got', answer)
answer = GetAnswerWithUnderscores('quick', 'rstlne')
print('GetAnswerWithUnderscores #2: expected _____, got', answer)
###Exercise 2
def GetWordSeparatedBySpaces(word):
new_string=''
for char in word:
new_string+=char+' '
return new_string[0:len(new_string)-1]
answer = GetWordSeparatedBySpaces('plane')
print('GetWordSeparatedBySpaces #1: expected p l a n e, got', answer)
answer = GetWordSeparatedBySpaces('to')
#Don't worry about the hasattr function, the if statement, or what they
#do: it's not required for this Hangman assignment.
if hasattr(answer, 'strip') and answer.strip() == answer:
text = 'PASS'
else:
text = 'FAIL'
print('GetWordSeparatedBySpaces #2: expected no spaces at the beginning or end,', text)
###Exercise 3
def IsWinner(answer,letters_guessed):
new_word=''
for each_letter in answer:
if each_letter in letters_guessed:
new_word+=each_letter
else:
return False
if new_word==answer:
return True
else:
return False
answer = IsWinner('plane', 'ranedlp')
print('IsWinner #1: expected True, got', answer)
answer = IsWinner('plane', 'plan')
print('IsWinner #2: expected False, got', answer)
###Exercise 4:
def IsLegalGuess(current_guess,letters_guessed):
if len(current_guess)!=1:
print('The letters guessed must not be more than one character long')
return False
elif current_guess.isalpha()==False:
print('Your guess must include only alphabet not numbers and symbols')
return False
elif current_guess in letters_guessed:
print('You cannot repeat the character that you have previously guessed')
return False
else:
return True
answer = IsLegalGuess('g', '')
print('IsLegalGuess #1: expected True, got', answer)
answer = IsLegalGuess('g', 'rstle')
print('IsLegalGuess #1: expected True, got', answer)
answer = IsLegalGuess('bb', 'cat')
print('IsLegalGuess #1: expected False, got', answer)
answer = IsLegalGuess('p', 'yzpr')
print('IsLegalGuess #1: expected False, got', answer)
#Exercise 5
def GetLegalGuess(letters_guessed):#abc
new_guess=input('guess a letter')
while IsLegalGuess(new_guess,letters_guessed)==False:
new_guess=input('guess a letter')
return new_guess
answer=GetLegalGuess('abc')
print('Your guess is',answer,'and it is legal')
###Exercise 6
def IsGuessRevealing(answer, legal_letter_guess):
if legal_letter_guess in answer:
return True
else:
return False
answer = IsGuessRevealing('welcome', 'c')
print('IsGuessRevealing #1: expected True, got', answer)
answer = IsGuessRevealing('quick', 'z')
print('IsGuessRevealing #1: expected False, got', answer)
# Part 2
#Exercise 2
def GetAllEnglishWords():
file=open('hangman_english_words.txt')
new_list=[]
for each_word in file:
no_space=each_word.strip()
new_list.append(no_space)
return new_list
answer = GetAllEnglishWords()
new_list = answer
if answer != None and 'voting' in answer:
text = 'PASS'
else:
text = 'FAIL'
print('GetAllEnglishWords #1: expected the word voting', text)
if answer != None and 'triage' not in answer:
text = 'PASS'
else:
text = 'FAIL'
print('GetAllEnglishWords #2: did not expect triage', text)
#Exercise 3
def GetRandomWord(words):
import random
my_index=random.randint(0,len(words)-1)
any_word=words[my_index]
return any_word
words = ['train', 'plane', 'automobile']
answer = GetRandomWord(words)
print('GetRandomWord #1: expected a random word,', answer in words)
#Exercise 4
def Play(answer):
import random
word=random.choice(new_list)
word=word.lower()
strikes=0
revealed=list(['_']*len(word))
guessed=[]
print('You have ',strikes,' strikes.')
print(' '.join(revealed))
while strikes<5:
if '_' not in revealed:
print('You Won')
return True
new_guess=input('enter a letter:')
if new_guess in guessed:
print('You have already guessed',new_guess,'. guess again.')
else:
guessed.append(new_guess)
if new_guess in word:
print('Good guess! Continue.')
for x in range(0,len(word)):
if (word[x])==new_guess:
revealed[x]=new_guess
print(' '.join(revealed))
else:
strikes+=1
print(new_guess,' is not in the word. You have ',strikes,' strike')
print('You have lost')
return False
Play(answer)
##Exercise 5
import random
rand_word = random.choice(new_list)
def StartupAndPlay (rand_word):
ask_user = input('Would you like to play again ?')
if ask_user != 'y':
return 'Byebye'
while ask_user == 'y':
Play(rand_word)
ask_user = input('Would you like to play again ?')
if ask_user == 'n':
break
else:
Play(any_word)
StartupAndPlay (rand_word)
#Execise 6
if __name__ == "__main__":
StartupAndPlay(rand_word)
#Extra credit 1
def GetPlayRecord():
play_record=[]
file=open('hangman_play_record.txt','r')
for each in file:
for number in each.split(','):
play_record.append(int(number))
return play_record
ans = GetPlayRecord()
if ans != None and isinstance(ans[0], int) and isinstance(ans[1], int):
text = 'PASS'
else:
text = 'FAIL'
print('GetPlayRecord #1: expected int elements,', text)
No comments:
Post a Comment