I am writing code following a book published ~June 2017, called Computer Coding Python Projects (for kids) by Carol Vorderman. Currently I am building a program which should function like hangman. I have four main difficulties (one, two, three, and five) and only number one seems to work. The error message I get when trying out difficulty number two is: Traceback (most recent call last):
File "...", line 88, in unknownletters = updateclue(guess, secretword, clue, unknownletters) TypeError: updateclue() takes 3 positional arguments but 4 were given
This error message only shows when I have entered a letter that is indeed in the word, like the vowel "a". The reason this boggles me is that the code used in difficulty number two is almost an exact copy of number one, except for the amount of letters the words have. The following is the code for difficulty number two.
lives = 5
words = ['false', 'fairy', 'teeth', 'shirt', 'otter',
'plane', 'plain', 'pulse', 'pizza', 'water',
'doggy', 'plane', 'brush', 'horse', 'light']
secretword = random.choice(words)
clue = list('?????')
heartsymbol = u'\u2764'
guessedwordcorrectly = False
unknownletters = len(secretword)
def updateclue(guessedletter, secretword, clue):
index = 0
while index < len(secretword):
if guessedletter == secretword[index]:
clue[index] = guessedletter
unknownletters = unknownletters - 1
index = index + 1
return unknownletters
while lives > 0:
print(clue)
print('Lives left:' + heartsymbol * lives)
guess = input('Guess a letter, or the whole word: ')
if guess == secretword:
guessedwordcorrectly = True
break
if guess in secretword:
unknownletters = updateclue(guess, secretword, clue, unknownletters)
else:
print('INCORRECT! You lost a life.')
lives = lives - 1
if unknownletters == 0:
guessedwordcorrectly = True
break
if guessedwordcorrectly:
print('You won! The secret word was ' + secretword)
print('You finished with ' + heartsymbol * lives)
else:
print('You lost! The secret word was ' + secretword)
More specifically, line 87 and 88 are: if guess in secretword: unknownletters = updateclue(guess, secretword, clue, unknownletters) I have copy+pasted the number one code into the number two box, so for reference, this is everything leading up to and including difficulty number one.
answer = input('Would you like to play? y/n ')
while answer == 'y':
difficulty = input('Choose difficulty: 1, 2, 3, or 5? ')
difficulty = int(difficulty)
if difficulty == 1:
lives = 5
words = ['boat', 'lamp', 'well', 'lips', 'help',
'last', 'kiss', 'cast', 'coop', 'make',
'port', 'cube', 'jazz', 'list', 'sure']
secretword = random.choice(words)
clue = list('????')
heartsymbol = u'\u2764'
guessedwordcorrectly = False
unknownletters = len(secretword)
def updateclue(guessedletter, secretword, clue, unknownletters):
index = 0
while index < len(secretword):
if guessedletter == secretword[index]:
clue[index] = guessedletter
unknownletters = unknownletters - 1
index = index + 1
return unknownletters
while lives > 0:
print(clue)
print('Lives left:' + heartsymbol * lives)
guess = input('Guess a letter, or the whole word: ')
if guess == secretword:
guessedwordcorrectly = True
break
if guess in secretword:
unknownletters = updateclue(guess, secretword, clue, unknownletters)
else:
print('INCORRECT! You lost a life.')
lives = lives - 1
if unknownletters == 0:
guessedwordcorrectly = True
break
if guessedwordcorrectly:
print('You won! The secret word was ' + secretword)
print('You finished with ' + heartsymbol * lives)
else:
print('You lost! The secret word was ' + secretword)
Can anyone find the answer to my problem? I am close to giving up.
Aucun commentaire:
Enregistrer un commentaire