Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Life counter not working
#1
So I'm trying to make hangman but the lives lost never go past 1, here's my code:


import random
livesLost = 0
words=["forrest","donkey","pineapple","chicken","cat","elephant","zebra","headphones","book","trees","minecraft","spiderman","window","house","batman","car","computer","jazz","japan","fortnite","blue","purple","alphabet","money","highlighter","speaker","hangman","globe","earth","river","moustache","test","table","pancakes","chocolate","superman","keyboard"]
def wordChoice(words):
    word=random.choice(words)
    return(word)

def maskGen(myWord):
    lengthOfWord=len(myWord)
    x = 0
    mask = ""
    while x < lengthOfWord:
        mask = mask + "_"
        x = x +1
    return(mask)

def guess(myWord,wordMask,lengthOfWord,livesLost):
    guess=input("Guess a letter: ")
    correct = False
    for i in range(lengthOfWord):
        if myWord[i] == guess:
            wordMask = wordMask[:i] + guess + wordMask[i+1:]
            correct = True
    if not correct:
        livesLost = livesLost + 1
        print("Life Lost")
        print(livesLost)
    return livesLost
    print(wordMask)
    return wordMask

def wordComplete(wordMask,myWord,lengthOfWord):
    
    for i in range(lengthOfWord):
        if wordMask[i]=="_":
            return False

    return True

print("You have 9 lives total, just like a cat, until the man is hung")

myWord=wordChoice(words)
lengthOfWord=len(myWord)

wordMask= maskGen(myWord)
print(wordMask)

wordFinished = False
        
print(myWord)
while wordFinished == False:
    wordFinished = wordComplete(wordMask,myWord,lengthOfWord)
    wordMask = guess(myWord,wordMask,lengthOfWord,livesLost)
    livesLost = guess(myWord,wordMask,lengthOfWord,livesLost)
print("You have guessed the word, well done!")
print("You lost",livesLost,"lives")
Reply
#2
The code gives
Error:
TypeError: 'int' object is not subscriptable
The function guess is called twice to get the return of wordMask and livesLost.
It currently only returns livesLost (the first return it gets to)
To make it work you could change the code to only call guess once and return both wordMask and livesLost

def guess(myWord,wordMask,lengthOfWord,livesLost):
    ...
    ...
    if not correct:
        livesLost = livesLost + 1
        print("Life Lost")
        print(livesLost)
    return livesLost, wordMask
    # print(wordMask)
    # return wordMask

...
...
while wordFinished == False:
    wordFinished = wordComplete(wordMask,myWord,lengthOfWord)
    # wordMask = guess(myWord,wordMask,lengthOfWord,livesLost)
    livesLost, wordMask = guess(myWord,wordMask,lengthOfWord,livesLost)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Optimize a game of life (4 sec for python when 6 ms for C version !) fatango 0 1,645 Jan-06-2021, 09:13 PM
Last Post: fatango
  Problem in a path finding algorithm (counter is not working) Kowalski 3 3,219 Feb-05-2018, 01:23 PM
Last Post: Gribouillis
  Keep a file locked for the life of an application rachelrosemond 2 3,087 Oct-03-2017, 04:37 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020