Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Having issues with lives
#1
Trying to make a hangman type game but the live counter goes down even if you input a letter correctly!

import random       
import time
L1 = 0
L2 = 0
L3 = 0
L4 = 0
L5 = 0
L6 = 0
F1 = "-"
F2 = "-"
F3 = "-"
F4 = "-"
F5 = "-"
F6 = "-"
C1 = 1
C2 = 1
C3 = 1
C4 = 1
C5 = 1
C6 = 1
Word = 0
Ans = 0
UserI = 0
UserpreI = 0
Cmds = 0
Lives = 4
LivesC = 0
LivesCo = 0
Loop = True

Word = random.randint(1, 3)
if Word == 1:
    L1 = "d"
    L2 = "r"
    L3 = "y"
    L4 = "i"
    L5 = "n"
    L6 = "g"
elif Word == 2:
    L1 = "j"
    L2 = "u"
    L3 = "m"
    L4 = "b"
    L5 = "l"
    L6 = "y"
elif Word == 3:
    L1 = "s"
    L2 = "k"
    L3 = "r"
    L4 = "u"
    L5 = "m"
    L6 = "p"

while Loop:
    UserpreI = int(input("What would you like to do?\n1)enter a letter\n2)Commands\n"))
    if UserpreI == 1:
        UserI = input("Enter a lowercase letter: ")
        if C1 == 1:
            if UserI == L1:
                print(f"You guessed {UserI} You got a letter correct")
                time.sleep(0.2)
                print(f"You have {Lives} lives left!")
                F1 = L1
                time.sleep(0.2)
                print(f"Word {F1}{F2}{F3}{F4}{F5}{F6}")
                C1 = 0
            else:
                LivesC = 1


        if C2 == 1:
            if UserI == L2:
                print(f"You guessed {UserI} You got a letter correct")
                time.sleep(0.2)
                print(f"You have {Lives} lives left!")
                F2 = L2
                time.sleep(0.2)
                print(f"Word: {F1}{F2}{F3}{F4}{F5}{F6}")
                C2 = 0
            else:
                LivesC = 1


        if C3 == 1:
            if UserI == L3:
                print(f"You guessed {UserI} You got a letter correct")
                time.sleep(0.2)
                print(f"You have {Lives} lives left!")
                F3 = L3
                time.sleep(0.2)
                print(f"Word: {F1}{F2}{F3}{F4}{F5}{F6}")
                C3 = 0
            else:
                LivesC = 1


        if C4 == 1:
            if UserI == L4:
                print(f"You guessed {UserI} You got a letter correct")
                time.sleep(0.2)
                print(f"You have {Lives} lives left!")
                F4 = L4
                time.sleep(0.2)
                print(f"Word: {F1}{F2}{F3}{F4}{F5}{F6}")
                C4 = 0
            else:
                LivesC = 1


        if C5 == 1:
            if UserI == L5:
                print(f"You guessed {UserI} You got a letter correct")
                time.sleep(0.2)
                print(f"You have {Lives} lives left!")
                F5 = L5
                time.sleep(0.2)
                print(f"Word: {F1}{F2}{F3}{F4}{F5}{F6}")
                C5 = 0
            else:
                LivesC = 1


        if C6 == 1:
            if UserI == L6:
                print(f"You guessed {UserI} You got a letter correct")
                time.sleep(0.2)
                print(f"You have {Lives} lives left!")
                F6 = L6
                time.sleep(0.2)
                print(f"Word: {F1}{F2}{F3}{F4}{F5}{F6}")
                C6 = 0
            else:
                LivesC = 1
    
    
    if LivesC == 1:
        Lives = Lives - 1
        print(f"You have {Lives} lives")
        LivesC = 0
            
                
    if Lives == 0:
        print("You have been hung...")
        time.sleep(1)
        Loop = False

            
    elif UserpreI == 2:
        Cmd = input("")
        if Cmd == "debug1":
            print(Word)
            print(f"{L1}{L2}{L3}{L4}{L5}{L6}")
        elif Cmd == "debug2":
            Loop = False
        elif Cmd == "commands":
            print("debug1 = provides the word and the number\ndebug2 = ends the program")
        elif Cmd == "debug3":
            print(Lives)
        elif Cmd == "debug4":
            print(LivesC)
Reply
#2
Put the "LivesC" thing only on the last if. Also the elif for "UserpreI" right now is being used as an elif for Lives == 0. One last thing, this code could really be cleaned up with function and checks. For example line 57. Instead of having them answer with a lowercase letter, just say "enter a letter" and use "string.lower()" where string is the string to be lowered. Another check could be
if len(UserpreI) != 1:
    #code

One last thing, people are more likely to help small code. Minimize the code down to the error. Ex -
Var = 10
for x in range(1, 10):
    print('Hello')
    print('Var = {Var}')
I would get an error about line 4, so...
print('Var = {Var}')
is my code for fixing. Just in case...
Full code below -
Var = 10
for x in range(1, 10):
    print('Hello')
    print('Var = {Var}')
Reply


Forum Jump:

User Panel Messages

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