By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,767 Members | 2,114 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,767 IT Pros & Developers. It's quick & easy.

Nim game for python

P: 1
I need help with this game I'm trying to do for my class out of a python book. This is what is says in the book. A two-player version of the game Nim. In the game, players take turns removing from 1 to 4 sticks from a pile of 13. The player who picks up the last stick wins the game. Your program should validate the input from the players. This means that the program should continue to ask a player for the number of sticks he or she wishes to take as any of the following are true:

- The number of sticks the player asks to take is greater than the number of sticks left.

- The number of sticks the player asks to take is greater than 4, the maximum number that he or she is allowed to take.

- The number of sticks the player asks to take is less than 1, the minimum number that he or she is allowed to take.

This is what i have -


print "Welcome to Nim."

sticks = 13
picks = int(raw_input("How many sticks would you like to pick? "))

while (sticks != youwin):
if picks == 1:
print "you have picked ", picks,"stick there are", sticks - 1, \
"sticks left."

if picks > 4:
print "illegal guess"

elif picks == 2:
print "you have picked ", picks,"sticks, there are", sticks - 2, \
"sticks left."

print raw_input("Pick another number.")

elif picks == 3:
print "you have picked ", picks,"sticks, there are", sticks - 3, \
"sticks left."

print raw_input("Pick another number.")

if sticks == 0:
print "you win"

raw_input("exit")
Mar 15 '08 #1
Share this Question
Share on Google+
2 Replies


Subsciber123
P: 87
First of all, use code tags like this:
Expand|Select|Wrap|Line Numbers
  1. print "Welcome to Nim."
  2.  
  3. sticks = 13
  4. picks = int(raw_input("How many sticks would you like to pick? "))
  5.  
  6. while (sticks != youwin):
  7.     if picks == 1:
  8.         print "you have picked ", picks,"stick there are", sticks - 1, "sticks left."
  9.  
  10.     if picks > 4:
  11.         print "illegal guess"
  12.  
  13.     elif picks == 2:
  14.         print "you have picked ", picks,"sticks, there are", sticks - 2, "sticks left."
  15.         print raw_input("Pick another number.")
  16.  
  17.     elif picks == 3:
  18.         print "you have picked ", picks,"sticks, there are", sticks - 3, "sticks left."
  19.         print raw_input("Pick another number.")
  20.  
  21.     if sticks == 0:
  22.         print "you win"
  23.  
  24. raw_input("exit")
Mar 15 '08 #2

Subsciber123
P: 87
Okay, now for the real problem:
First, you only set the value of picks once.
Second, you never define the value of youwin.
Third, it is bad form to use parens around expressions when they aren't needed.

Try this:
Expand|Select|Wrap|Line Numbers
  1. print "Welcome to Nim."
  2. sticks=13
  3. over=False
  4. while not over: # while the game is not over
  5.     pick=0
  6.     while not 1<=pick and not pick<=4 and not sticks<pick:
  7.         # while your conditions are not satisfied
  8.         try:
  9.             pick=int(raw_input("How many sticks would you like to pick? "))
  10.         except ValueError: # if the user doesn't enter a number, int() raises ValueError
  11.             pass
  12.     sticks-=pick # subtract pick from sticks
  13.     print "You have %d sticks remaining."%sticks # formatted string
  14.     if sticks==0:
  15.         over=True # could also use "break" instead of this flag
  16. raw_input("You win!")
Mar 15 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.