Python Forum
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need assistant!
#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.

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]

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:
			x = 1
			cardgeneration()
			
def cardgeneration():
	with open('dogs.txt') as cards: #Opens the file and saves it as variable cards
		cards = f.read().splitlines() #Reads the text file and seperates them
		cards = [line.split(",") for line in cards #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
	middle = pilenumber / 2 
	playerdeck =[0,middle-1]
	computerdeck = [middle,pilenumber]
	plength = len(playerdeck)
	clength = len(computerdeck)

	#Here should be a line of code to select a name from the created array.
			   
	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
Reply
#2
You could improve it by fixing the syntax error on line 56. Note that print("categorychoice") prints the text 'categorychoice', not what is in the categorychoice variable. Also, triple quoted strings allow embedded new lines:

categorychoice = """Please select a category you wish to compare.
1 - Excerise
2 - Friendliness
3 - Intelligence
4 - Drool"""
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(Oct-06-2018, 12:47 AM)ichabod801 Wrote: You could improve it by fixing the syntax error on line 56. Note that print("categorychoice") prints the text 'categorychoice', not what is in the categorychoice variable. Also, triple quoted strings allow embedded new lines:
 categorychoice = """Please select a category you wish to compare. 1 - Excerise 2 - Friendliness 3 - Intelligence 4 - Drool""" 

Whoops, I forgot about that. I appreciate it.

Also, When I run it a syntax error occurred on line 60 when it finds the middle of pilenumber.
Reply
#4
Please, always post full traceback in error tags.
In this case you miss a closing parenthesis on previous line (56)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
I'm not getting a syntax error after the one on line 56 is corrected. Please post your current code for the cardgeneration function.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
Lines 24 and 25: If these sequences are not going to be changed by the program, these should be tuples; tuples are more performant.

Line 27: change menu() name. This causes a problem with the menu variable defined on line 7 because they have the same name. On my machine, this printed the memory location of the function rather than the menu. A good practice is for functions/methods to have verbal names (e.g. showMenu or show_menu).

Line 37: causes an error because x has not be assigned before check

Line 44: use a modulus instead of a list to determine if the number is even (i.e. number % 2 != 0).

Line 56: close the list comprehension with a "]"
Reply


Forum Jump:

User Panel Messages

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