I have been creating a scoring program for a game of hearts. If there is a an even number of players then players will pass across. And an odd number there is no pass across. I created these 2 functions that work fine. But I keep scratching my head trying to simplify them into only 1 function. I know its possible but I just can't get it right. This is the block of code I'm working with:
#this function is for an odd number of players
def no_across():
global pass_counter
if pass_counter == 1:
print("WE ARE PASSING LEFT")
pass_counter = pass_counter + 1
elif pass_counter == 2:
print("WE ARE PASSING RIGHT")
pass_counter = pass_counter + 1
elif pass_counter == 3:
print("WE ARE HOLDING")
pass_counter = pass_counter - 2
#this function is for an even number of players
def yes_across():
global pass_counter
if pass_counter == 1:
print("WE ARE PASSING LEFT")
pass_counter = pass_counter + 1
elif pass_counter == 2:
print("WE ARE PASSING RIGHT")
pass_counter = pass_counter + 1
elif pass_counter == 3:
print("WE ARE PASSING ACROSS")
pass_counter = pass_counter + 1
elif pass_counter == 4:
print("WE ARE HOLDING")
pass_counter = pass_counter - 3
#determines which of the 2 previous functions to call
if player_count % 2 == 0:
yes_across()
else:
no_across()
As you can see in the first block and second block I have repeated code but I know it can be simplified. All help is appreciated.
EDIT: Below is the complete code. Don't judge me because I just whipped it together last night as a test to see if it would work. Also I am integrating this into a different project I have that will use a GUI. I just needed a rough copy of the program to work.
#this program is meant to keep track of score during a game of hearts
#displays a greeting
print("Welcome to the player score keeper!")
#score limit reached allows game to continue until changed
score_limit_reached = False
#these are the players names
John = 0
Elaine = 0
Austin = 0
Bridgette = 0
Rocky = 0
#these following variables help determine if the program should have certain
#passing in game. also keeps track of score limit.
player_count = 0
player_count = int(input("How many players are going to play? "))
score_limit = 0
pass_counter = 1
score_limit = int(input("What is the score limit? "))
#this function is for an odd number of players
def no_across():
global pass_counter
if pass_counter == 1:
print("WE ARE PASSING LEFT")
pass_counter = pass_counter + 1
elif pass_counter == 2:
print("WE ARE PASSING RIGHT")
pass_counter = pass_counter + 1
elif pass_counter == 3:
print("WE ARE HOLDING")
pass_counter = pass_counter - 2
#this function is for an even number of players
def yes_across():
global pass_counter
if pass_counter == 1:
print("WE ARE PASSING LEFT")
pass_counter = pass_counter + 1
elif pass_counter == 2:
print("WE ARE PASSING RIGHT")
pass_counter = pass_counter + 1
elif pass_counter == 3:
print("WE ARE PASSING ACROSS")
pass_counter = pass_counter + 1
elif pass_counter == 4:
print("WE ARE HOLDING")
pass_counter = pass_counter - 3
#here begins the actual scoring section of the game
while score_limit_reached == False:
#determines which of the 2 previous functions to call
if player_count % 2 == 0:
yes_across()
else:
no_across()
#user input keeps track of score
John = John + int(input("john: "))
Elaine = Elaine + int(input("elaine: "))
Austin = Austin + int(input("austin: "))
Bridgette = Bridgette + int(input("bridgette: "))
Rocky = Rocky + int(input("rocky: "))
#prints current score of each player
print("\n\nJohn's score - " + str(John))
print("Elaine's score - " + str(Elaine))
print("Austin's score - " + str(Austin))
print("Bridgette's score - " + str(Bridgette))
print("Rocky's score - " + str(Rocky) + "\n\n")
#if the score limit has been reached it will end the while loop
if John >= score_limit:
score_limit_reached = True
if Elaine >= score_limit:
score_limit_reached = True
if Austin >= score_limit:
score_limit_reached = True
if Bridgette >= score_limit:
score_limit_reached = True
if Rocky >= score_limit:
score_limit_reached = True
#prints the final score for each player
print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nJohn - " + str(John))
print("Elaine - " + str(Elaine))
print("Austin - " + str(Austin))
print("Bridgette - " + str(Bridgette))
print("Rocky - " + str(Rocky) + "\n")
#if any number of players has lost will declare each
#player a poopy, followed by which score was the highest
if John >= score_limit:
print("John is a poopy!")
if Elaine >= score_limit:
print("Elaine is a poopy!")
if Austin >= score_limit:
print("Austin is a poopy!")
if Bridgette >= score_limit:
print("Bridgette is a poopy!")
if Rocky >= score_limit:
print("Rocky is a poopy!")
biggest_poopy = str(max(John, Elaine, Austin, Bridgette, Rocky))
print("\nThe biggest poopy is " + biggest_poopy + "!")
'''
s since those start a multi-line comment in python, though it seemed the intention was for a code fence to delimit a code block. Code fences are three or more backticks (i.e.`
) or tildes (i.e.~
) \$\endgroup\$