Sunday, December 13, 2015
#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)
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment