mercredi 11 août 2021

Throwing list index error at random times in python

I'm working on Card-Jitsu, I've basically finished most of it, but I have an error that I'm not able to fix, any fixes? I'm trying to do something with import since I haven't used it yet.

Card Generator code:

#Generator that generates the cards for YOU and COMP
n = 0
helper = 0
n2 = 0
helper2 = 0

cards = ["Fire 1", "Fire 2", "Fire 3", "Water 1", "Water 2", "Water 3", "Thunder 1", "Thunder 2", "Thunder 3"]
userDeck = ["", "", "", ""]
compDeck = ["", "", "", ""]

while n < 4:
    import random
    cardNum = random.randint(0, 8)
    userDeck[helper] = cards[cardNum]
    n = n + 1
    helper = helper + 1

while n2 < 4:
    import random
    cardNum = random.randint(0, 8)

    compDeck[helper2] = cards[cardNum]
    n2 = n2 + 1
    helper2 = helper2 + 1

The main code:

print("Welcome to Card-Jitsu Type PLAY to continue!")
print("This game is a luck based game, it's basically a copy of Rock-Paper-Scissors.Fire > Thunder, Thunder > Water, Water > Fire. Let's play now!")
start = input()
from cardGen import compDeck
from cardGen import userDeck

userScore = 0
compScore = 0

while len(userDeck) or len(compDeck) > 0:

    if start == "PLAY": #Fix final error
        import random
        compCardNum = random.randint(0, len(compDeck))
        
        print("Your deck contains", userDeck, "| Score: You -", userScore, ", Comp -", compScore) 
        user = int(input("Pick a number (0 for 1st card, 1 for next card, and so on): "))
        print("You picked", userDeck[user])
        print("Comp picked", compDeck[compCardNum])
        curCard = userDeck[user]

        if curCard in userDeck:
            if curCard.startswith("Thunder") and compDeck[compCardNum].startswith("Water"):
                print("You win!")
                userDeck.pop(user)
                compDeck.pop(compCardNum)
                userScore += 1
                continue
            
            elif curCard.startswith("Water") and compDeck[compCardNum].startswith("Fire"):
                print("You win!")
                userDeck.pop(user)
                compDeck.pop(compCardNum)
                userScore += 1
                continue

            elif curCard.startswith("Fire") and compDeck[compCardNum].startswith("Thunder"):
                print("You win!")
                userDeck.pop(user)
                compDeck.pop(compCardNum)
                userScore += 1
                continue

            elif curCard.startswith("Water") and compDeck[compCardNum].startswith("Thunder"):
                print("You lost!")
                userDeck.pop(user)
                compDeck.pop(compCardNum)
                compScore += 1
                continue

            elif curCard.startswith("Fire") and compDeck[compCardNum].startswith("Water"):
                print("You lost!")
                userDeck.pop(user)
                compDeck.pop(compCardNum)  
                compScore += 1
                continue  

            elif curCard.startswith("Thunder") and compDeck[compCardNum].startswith("Fire"):
                print("You lost!")
                userDeck.pop(user)
                compDeck.pop(compCardNum)
                compScore += 1
                continue

            elif len(compDeck) <= 0:
                print("Comp has no cards left")

            else: 
                temp1 = curCard.split(" ")
                temp2 = compDeck[compCardNum].split(" ")

                h1 = int(temp1[1])
                h2 = int(temp2[1])

                if h1 > h2:
                    print("You win!")
                    userScore += 1

                else:
                    print("You lost!")
                    compScore += 1

                userDeck.pop(user)
                compDeck.pop(compCardNum)
                continue

        else : print(":/")

The error I'm getting:

line 26, in <module>
    print("Comp picked", compDeck[compCardNum])
IndexError: list index out of range

Not sure how to fix it, if anyone knows please tell me. Thanks :D




Aucun commentaire:

Enregistrer un commentaire