We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,644 Members — Technology Publication meets Social Media

# Pickup Sticks game

so i have an assignment for a class to make a pick up sticks game. This is the actual assignment:
"The rules of pick up sticks are as follows: The user chooses the number of matchsticks (5 to 50) to place in a pile. Then, the computer chooses who will go first. At each turn, the contestant can remove one, two, or three matchsticks from the pile. The contestant who chooses the last matchstick loses. The computer should make the user always select from a pile where the number of matchsticks has a remainder of 1 when divided by 4. For instance, if the user initially chooses a number of matchsticks that has a remainder of 1 when divided by 4, then the computer should have the user go first. Otherwise, the computer should go first and remove the proper number of matchsticks. (Note: the remainder when n is divided by 4 is n % 4.) If you have written this program correctly, the computer should win every game."

My main question is how am i to make the program chose the "proper number of sticks"? and also if you see that I did something wrong or could do something better, let me know!

``````def count(sticks):
cnt=""
if sticks>1:
print("There are currently", sticks, "sticks on the pile.")
elif sticks==1:
print("There is currently 1 stick on the pile.")
while sticks>0:
cnt+="|"
sticks-=1
print("Current status of the pile:")
print("*",cnt,"*")
cnt=""

def computer(sticks):
print("I will pick up 1 stick")
sticks-=1
return sticks

def game(sticks):
while sticks>0:
if sticks>=5 and sticks<=50:
if sticks%4==1:
print("You can go first.")
count(sticks)
choice=int(input("How many sticks would you like to pick up? "))
if choice<=sticks:
sticks-=choice
count(sticks)
else:
choice=int(input("How many sticks would you like to pick up? "))
if choice<=sticks:
sticks-=choice
count(sticks)
print("I will pick up 1 stick")
choice-=1
count(sticks)
if sticks==1:
choice=int(input("How many sticks would you like to pick up? "))
if choice==1:
sticks-=choice
else:
choice=int(input("How many sticks would you like to pick up? "))
if choice==1:
sticks-=choice
else:
print("I will go first.")
count(sticks)
print("I will pick up 1 stick.")
sticks-=1
count(sticks)
choice=int(input("How many sticks would you like to pick up?"))
if choice<4 and choice>0:
sticks-=choice
count(sticks)
print("I will pick up 1 stick")
choice-=1
count(sticks)
if sticks==1:
choice=int(input("How many sticks would you like to pick up? "))
if choice==1:
sticks-=choice
print("I will pick 3 sticks")
else:
choice=int(input("How many sticks would you like to pick up? "))
if choice==1:
sticks-=choice

else:
print("That number is not between 5 and 50. Please try again")
sticks=int(input("How many sticks would you like there to be in the pile? Choose a number between 5 and 50."))
game(sticks)

return

sticks=int(input("How many sticks would you like there to be in the pile? Choose a number between 5 and 50."))
game(sticks)
print("You picked up the last stick. I won!")``````

Thank you for the help!

3
Contributors
3
Replies
3 Hours
Discussion Span
1 Year Ago
Last Updated
4
Views
RoqueyB
Newbie Poster
13 posts since Sep 2011
Reputation Points: 10
Skill Endorsements: 0

Your display routine is at least too complicated and line 12 does not make sense.
Same shorter:

``````def count(sticks):
print("* %s *" % (sticks * '|'))

count(33)``````

Indents of lines 64 and 65 does not make sense.

I see for example "How many sticks would you like to pick up?" seven times in your code and just can not understand it.

pyTony
pyMod
Moderator
6,330 posts since Apr 2010
Reputation Points: 879
Skill Endorsements: 27

line 12 is to re-initiate cnt. So, that it always starts at nothing.
what does "count(33)" mean?

and lines 64 and 65 are part of a problem i've been trying to work out as well. I need it to print AFTER the other nested ifs, but in that if else statement. If I put it after the else statement then it'll print no matter what, and thats not necessarily what i want, but it does look awkward there.

RoqueyB
Newbie Poster
13 posts since Sep 2011
Reputation Points: 10
Skill Endorsements: 0

The computer should make the user always select from a pile where the number of matchsticks has a remainder of 1 when divided by 4.

``````import random

# choose some random numbers and the appropriate number to pick up
# so that the number of sticks = num*4+1
# no clue given for when the user chooses so the number of sticks = num*4+1
for ctr in range(5):
num_of_sticks = random.randint(5, 50)
print "%d sticks in the pile" % (num_of_sticks)
whole, remain = divmod(num_of_sticks, 4)
if remain == 0:
remain=4
print "     The computer will pick up %d sticks" % (remain-1)``````

You can also use a function that contains an infinite loop to get the input.

``````def get_choice(sticks):
while True:
choice=int(input("How many sticks would you like to pick up (1, 2, or 3)? "))
#    if choice<=sticks:    ## doesn;t choice have to be 1,2 or 3?
if 0 < choice < 4 and choice <= sticks:
return choice

else:
woooee
Posting Maven
2,715 posts since Dec 2006
Reputation Points: 827