My Python project for school. Need help!


#1

So I was tasked to create a Card Comparison Game, named ‘Celebrity Dogs’ (Funny name right?), I’ve so far used my basic knowledge on Python, I’m not very good at it yet but getting there. I wanna know if there’s any way to improve it or go over any errors that I haven’t been able to figure out.

Here’s is what in the dogs.txt file:

Annie the Afgan Hound,
Bertie the Boxer,
Betty the Borzoi,
Charlie the Chihuahua,
Chaz the Cocker Spaniel,
Donald the Dalmatian,
Dottie the Doberman,
Fern the Fox Terrier,
Frank the French Bulldog,
George the Great Dane,
Gertie the Greyhound,
Harry the Harrier,
Ian the Irish Wolfhound,
Juno the Jack Russell,
Keith the Kerry Blue,
Larry the Labrador,
Marge the Maltese,
Max the Mutt,
Nutty the Newfoundland,
Olive the Old English Sheepdog,
Peter the Pug,
Poppy the Pekingese,
Rosie the Rottweiler,
Ruby the Retriever,
Sam the Springer Spaniel,
Sukie the Saluki,
Vernon the Vizsla,
Whilma the West Highland Terrier,
William the Whippet,
Yolande the Yorkshire Terrier,

#Celebrity Dog Games
"""
The progam is a similar to the card game 'Trump Card' Where the players compare cards to see which chosen value is higher than their opponents. The aim of the program is to allow the user to play the game agaisnt the computer
and that the cards generated are placed into arrays and hopefully have it choose a random name that hasn't been used.

"""
import random #Allows the use to randomly choose.
menu = "Welcome to Celebrity Dog Game!\n\
Please Select an Option:\n\
1 - Begin Game\n\
2 - Quit\n\
Option: "
 
categorychoice = """Please select a category you wish to compare.\n\
1 - Excerise\n\
2 - Friendliness\n\
3 - Intelligence\n\
4 - Drool"""
 
x=0
a=0
playerspile = 0
computerpile = 0
 
cardchoice = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30]
multiple2 = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30]

menu()
def menu(): #Defines the Menu
    while a == 0:
        option = int(input(menu)) #Gives the user the option to leave or quit the game
        if option == 2:
          print("Goodbye, please come again!")
          raise SystemExit
        elif option == 1:
          cardselection()
           
def cardselection():
    while x == 0:
        print("Please select a number between 4 and 30 and a multiple of two.")
        pilenumber=int(input("Number of cards: ")) #Ask the users to imput their wanted Pile Size
 
        if (pilenumber < 4 and pilenumber >= 30):
            print("Please select a number between 4 to 30.") #Tells the user that the number is not between 4 and 30.
             
        elif pilenumber not in multiple2:
            print("Please select a number which is a multiple of two.") #Tells the user that the number is not a multiple of two.
 
        elif pilenumber not in cardchoice:
            print("Please select a valid number") #Tells the user that the input is not a number
        else:
            middle = (pilenumber/2)
            x = 1
            cardgeneration()
             
def cardgeneration():
    with open('dogs.txt') as dogname: #Opens the file and saves it as variable cards
        dogname = f.read().splitlines() #Reads the text file and seperates them
        dogname = [line.split(",") for line in dogname #This splits the list into arrays
        
    #Anything between this message to the next message generates the value for both the player's and the computer's first and other cards
    
    playerdeck = [0, middle,-1]
    computerdeck = [middle,pilenumber]
    plength = len(playerdeck)
    clength = len(computerdeck)
 
                
    p_excercise = random.randint(1,5) 
    p_intelligence = random.randint(1,100)
    p_friendliness = random.randint(1,10)
    p_drool = random.randint(1,10)
 
    c_excercise = random.randint(1,5) 
    c_intelligence = random.randint(1,100)
    c_friendliness = random.randint(1,10)
    c_drool = random.randint(1,10)
    #Anything between this message to the previous message generates the value for the cards.
    gamestart()
    
def gamestart():
    #Beyond this point is where the game begins.
    while y == 0:
            print("You have: ", plength, "Cards") #Prints out the player's card
            print("The Computer have: ",clength, " Cards")#Prints out the computer's deck size
                
            print("Your dog name is: " + dogname + " Excercise: " + p_excercise + " Friendliness:" + p_friendliness + "Intelligence: " + p_intelligence + " Drool:" + p_drool)#Prints out the player's first card
            print("Please know that the value of these cards are auto generated.")
            print(categorychoice)
            category = int(input("Please Select an Option: ")) #Tells the user to select a category from the list
            #Beneath this code is where the Chosen Category is being compared with the computer's card.
            if category == 1:
                print("You've chosen to compare your Excerise.")
                            
                if c_excercise > p_excercise:
                    print("Computer wins!")
                    playerdeck = playerdeck - 1
                    computerdeck = computerdeck + 1
                    cardgeneration()
                
            elif c_excercise <= p_excercise:
                    print("Player wins!")
                    playerdeck = playerdeck + 1
                    computerdeck = computerdeck - 1
                    cardgeneration()
                
            elif category == 2:
                print("You've chosen to compare your Friendliness.")
                if c_friendliness > p_friendliness:
                    print("Computer wins!")
                    playerdeck = playerdeck - 1
                    computerdeck = computerdeck + 1
                    cardgeneration()
                
                elif c_friendliness <= p_friendliness:
                    print("Player wins!")
                    playerdeck = playerdeck + 1
                    computerdeck = computerdeck - 1
                    cardgeneration()
                
            elif category == 3:
                print("You've chosen to compare your Intelligence.")
                if c_intelligence > p_intelligence:
                    print("Computer wins!")
                    playerdeck = playerdeck - 1
                    computerdeck = computerdeck + 1
                    cardgeneration()
                
            elif c_intelligence <= p_intelligence:
                    print("Player wins!")
                    playerdeck = playerdeck + 1
                    computerdeck = computerdeck - 1            
                    cardgeneration()
                
            elif category == 4:
                print("You've chosen to compare your Drool.")
                if c_drool > p_drool:
                    print("Player wins!")
                    playerdeck = playerdeck + 1
                    computerdeck = computerdeck - 1
                    cardgeneration()
                
                elif c_drool <= p_drool:
                    print("Computer wins!")
                    playerdeck = playerdeck - 1
                    computerdeck = computerdeck + 1
                    cardgeneration()
                
            if playerdeck == pilenumber:
                print("Player wins the game!")
                y = 1
                leave()
            elif computerdeck == pilenumber:
                print("Computer wins the game!")
                y = 1
                leave()
             
def leave():
    #The player is given the option if they wish to play again or end the code by changing the 'a' variable to 1
    leave = input(("Would you like to play again or leave the game?"))
                
    if (leave == Y or leave == Y):
        print("Please come again!")
        raise SystemExit #Stops the whole code
    elif (leave == N or leave == n):
        print("Returning to main menu.")
        menu() #Returns to the main menu

#2

i put your code here:

https://repl.it/@stetim94/SandybrownDeepskyblueTahr

for me, the program is not even running. I get an error on line 63:

playerdeck = [0, middle,-1]

you try to access by index, but of which string or list? General syntax:

my_list[index]
my_string[index]

#3

I found the issue why the error kept on getting a Syntax Error. Just missed a second Square Bracket on line 59.


#5

I’ve changed the code and it seem to have some progress. There’s an error occuring and I’m not sure what it entirely means.


#6

here:

print("Your dog name is: " + dogname + " Excercise: " + p_excercise + " Friendliness:" + p_friendliness + "Intelligence: " + p_intelligence + " Drool:" + p_drool)

you get an error. It seems one of the variable you try to insert into the string is a list. You go about printing the variable to see which is a list:

print("""Beginning the game!""")
print(dogname)
print(p_excercise)

and so forth.


#7

That makes sense. Do I put random.choice(dogname) so that it doesn’t print out the whole list?


#8

but dogname is multi-dimensional (list within a list), why? Its possible, but then random.choice() still gives a list.

I haven’t studied the entire program yet, you would need to decide of a 2d list is what you are after for dogname


#9

Okay! I’ve finally got my code to work! So far there hasn’t been a error when i tested it out for a while.

#Celebrity Dog Games
"""
The progam is a similar to the card game 'Trump Card' Where the players compare cards to see which chosen value is higher than their opponents. The aim of the program is to allow the user to play the game agaisnt the computer
and that the cards generated are placed into arrays and hopefully have it choose a random name that hasn't been used.

"""
import random #Allows the use to randomly choose.


menu = """
--------------------------------------
Welcome to Celebrity Dog Game!
Please Select an Option:
1 - Begin Game
2 - Quit
Option: """
 
categorychoice = """
--------------------------------------
Please select a category you wish to compare.
1 - Excerise
2 - Friendliness
3 - Intelligence
4 - Drool
"""

 
x=0
a=0

 
cardchoice = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30]
multiple2 = [4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30]


           
def pilenumber():
    while True:
        print("---------------------------------------------------------------")
        print(("Please select a number between 4 and 30 and a multiple of two."))
        global pilenumber
        pilenumber=int(input("Number of cards: ")) #Ask the users to imput their wanted Pile Size
 
        if (pilenumber < 4 and pilenumber >= 30):
            print("Please select a number between 4 to 30.") #Tells the user that the number is not between 4 and 30.
             
        elif pilenumber not in multiple2:
            print("Please select a number which is a multiple of two.") #Tells the user that the number is not a multiple of two.
 
        elif pilenumber not in cardchoice:
            print("Please select a valid number") #Tells the user that the input is not a number

        else:
            return pilenumber
        
 
def cardgeneration():
    global dogname
    with open('dogs.txt') as dogname: #Opens the file and saves it as variable cards
        dogname = dogname.read().splitlines() #Reads the text file and seperates them
        dogname = [line.split(",") for line in dogname] #This splits the list into arrays

    return dogname

def gamestart():
    #Beyond this point is where the game begins.
    ppile = pilenumber/2
    cpile = pilenumber/2

    while True:
        p_excercise = random.randint(1,5) 
        p_intelligence = random.randint(1,100)
        p_friendliness = random.randint(1,10)
        p_drool = random.randint(1,10)
   
        c_excercise = random.randint(1,5)
        c_intelligence = random.randint(1,100)
        c_friendliness = random.randint(1,10)
        c_drool = random.randint(1,10)
        print("--------------------------------------")
        print("You have: ",ppile,"cards.")
        print("Computer have: ",cpile,"cards.")
        print("--------------------------------------")
        print("Your dog name is:")
        print(random.choice(dogname))
        print("Excercise: ",p_excercise)
        print("Friendliness: ",p_friendliness)
        print("Intelligence: ",p_intelligence)
        print("Drool: ",p_drool)
        print("--------------------------------------")
        #Prints out the player's first card
        y = 0
            
        while y == 0:
            print("(Please know that the value of these cards are auto generated.)")  
            print(categorychoice)

            category = int(input("Please Select an Option: ")) #Tells the user to select a category from the list
            #Beneath this code is where the Chosen Category is being compared with the computer's card.
            print("--------------------------------------")
            if category == 1:
                print("You've chosen to compare your Excerise.")
                print("Player: ", p_excercise, ".")
                print("Computer: ",c_excercise,".")
                if c_excercise > p_excercise:
                    print("Computer wins!")
                    ppile = ppile - 1
                    cpile = cpile + 1
                    print(p_excercise)
                    cardgeneration()
                    y = 1
                elif c_excercise <= p_excercise:
                    print("Player wins!")
                    ppile = ppile + 1
                    cpile = cpile - 1
                    cardgeneration()
                    y = 1
            elif category == 2:
                print("You've chosen to compare your Friendliness.")
                if c_friendliness > p_friendliness:
                    print("Computer wins!")
                    ppile = ppile - 1
                    cpile = cpile + 1
                    cardgeneration()
                    y = 1
                elif c_friendliness <= p_friendliness:
                    print("Player wins!")
                    ppile = ppile + 1
                    cpile = cpile - 1
                    cardgeneration()
                    y = 1
            elif category == 3:
                print("You've chosen to compare your Intelligence.")
                if c_intelligence > p_intelligence:
                    print("Computer wins!")
                    ppile = ppile - 1
                    cpile = cpile + 1
                    cardgeneration()
                    y = 1
                elif c_intelligence <= p_intelligence:
                    print("Player wins!")
                    ppile = ppile + 1
                    cpile = cpile - 1            
                    cardgeneration()
                    y = 1
            elif category == 4:
                print("You've chosen to compare your Drool.")
                if c_drool > p_drool:
                    print("Player wins!")
                    ppile = ppile + 1
                    cpile = cpile - 1
                    cardgeneration()
                    y = 1
                elif c_drool <= p_drool:
                    print("Computer wins!")
                    ppile = ppile - 1
                    cpile = cpile + 1
                    cardgeneration()
                    y = 1
            if ppile == pilenumber:
                print("Player wins the game!")
                leave()
            elif cpile == pilenumber:
                print("Computer wins the game!")
                leave()
            
def leave():
    #The player is given the option if they wish to play again or end the code by changing the 'a' variable to 1
    while True:
        leave = int(input("""
    Would you like to:
    1 - Play Again
    2 - Leave Game
    Option:  """))
                
        if leave == 2:
          print("Please come again!")
          raise SystemExit #Stops the whole code
        elif leave == 1:
            print("""
            -----------------
            Returning to Menu
            -----------------
            """)
            menu1()

        else:
            print("Pleases select again.")

def menu1():
    while True:
        option = int(input(menu)) #Gives the user the option to leave or quit the game
        if option == 2:
            print("Goodbye, please come again!")
            raise SystemExit
        elif option == 1:
            pilenumber()
            cardgeneration()
            gamestart()
        else:
            print("Please select again.")


while True:
    menu1()

#10

nice :slight_smile: Why is did you use a python2 repl? The code seems to be python3