I am creating a game of pick up sticks for a class. Our goal is to create a function that loops 4 other functions to run the game.
main():
while numb_sticks > 1:
func1()
func2()
func3()
func4()
else:
print("Player {} wins!")
My problem is keeping the values I changed from one function onto the next. I have numb_sticks declared outside my functions, as well as the starting player.
numb_sticks = 20
player = 1
I have print() throughout my functions to keep track of what values are changing. I have determined that as a function is completed, the numb_stick and player values revert to their original value outside the function. This essentially creates an endless loop where the number of sticks always reverts to 20 and the turn is always for player 1. I am required to use these two variables as parameters for my functions. I do not know how to fix this. Below is the entirety of my code. I put comments along the entire program to explain what I am doing.
pick_up_sticks.py
import random
def not_quite_right(numb_sticks): #This function creates a chance to add sticks to the game.
global sticks_added #Declaring variables global to be changed across program.
global sticks_remaining
print("Initial numb_sticks nqr:", numb_sticks) #Testing change.
n = random.randint(1,10) #This and the next line set up the %30 chance for more sticks.
if n > 7:
sticks_added = random.randint(1,4) #This sets up a random amount of sticks between 1-4 to be added.
if numb_sticks + sticks_added <= 20: #Checks if cap is exceeded.
numb_sticks += sticks_added #Adding sticks to pile.
print(numb_sticks) #Testing change, will remove on final.
return
else:
sticks_added = 20 - numb_sticks #This is a small work around if the sticks added would have been more than 20.
numb_sticks = 20
return
else: #The following complets the 70% chance of no sticks being added.
sticks_added = 0
return
"""
---
"""
def take_sticks(player, numb_sticks):
global sticks_taken #Setting variables as global for use across program.
global sticks_remaining
print("Initial numb_sticks ts:", numb_sticks) #Test before change.
if numb_sticks > 1: #Checks if game is still going.
sticks_taken = input("Player {}, choose how many sticks you would like to take: ".format(player)) #Lets player choose stick amount.
try: #Next 4 lines check if the input is a number and restarts turn if it isn't.
int(sticks_taken)
except ValueError:
print("Invalid input! Please type a number.")
take_sticks(player, numb_sticks)
sticks_taken = int(sticks_taken) #Sets input as integer.
if sticks_taken <= 0 or sticks_taken >= 4: #Next 3 lines check if inputted number is legal move and restarts turn if it isn't.
print("You can only take 1, 2, or 3 sticks.")
take_sticks(player, numb_sticks)
elif sticks_taken < 4 and sticks_taken > 0: #Determines legal move.
numb_sticks -= sticks_taken #Sets sticks remaining by removing sticks taken from original 20 sticks.
print("Numb_sticks:", numb_sticks) #Test change.
if player == 1: #Next 5 lines changes players turn.
player = 2
print("Next turn is player", player,"'s turn.'") #Test player change.
return
else:
player = 1
return
return
else:
return
else:
return
def display_board(numb_sticks): #This function states how many sticks there are at the start of the turn.
print("There are", numb_sticks, " sticks on the board.")
def display_summary(player, sticks_taken, sticks_added, numb_sticks): #The following function simply displays the remaining sticks and the next players turn.
print("Player", player,"took", sticks_taken, "sticks, pictsie added", sticks_added, "sticks back, leaving", numb_sticks, "sticks.")
def main(): #Creating a loop for the game.
numb_sticks = 20 #Setting the number of sticks.
player = 1 #Sets player 1 to start.
while numb_sticks > 1:
display_board(numb_sticks)
take_sticks(player, numb_sticks)
not_quite_right(numb_sticks)
display_summary(player, sticks_taken, sticks_added, sticks_remaining)
else:
print("Player {} has won the game!".format(player)) #Declares a player the winner!
main() #Begins the game!