473,395 Members | 1,629 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Nim game for python

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
2 13022
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
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

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

Similar topics

1
by: O'Neal Computer Programmer | last post by:
I was reading here: http://groups.google.com/groups?q=elemental+group:comp.lang.python.*&hl=en&lr=&ie=UTF-8&group=comp.lang.python.*&selm=mailman.1044572235.32593.python-list%40python.org&rnum=3...
1
by: BlackHawke | last post by:
Hello! My name is Nick Soutter, I am the owner of a small game company in San Diego, CA called Aepox Games (www.aepoxgames.net). Our first product, Andromeda Online...
4
by: The_Incubator | last post by:
As the subject suggests, I am interested in using Python as a scripting language for a game that is primarily implemented in C++, and I am also interested in using generators in those scripts... ...
4
by: Moosebumps | last post by:
When I have time, I am planning to evaluate Python for console game development (on Playstation 2, GameCube, and Xbox). Does anyone have any experience with this? Pretty much the only resource...
6
by: Alex Endl | last post by:
ok now that i know the random function, i made this guessing game. I get an error though, and Im new so im not to good at figuring out what its talking about. import random a =...
0
by: Michael Goettsche | last post by:
Hi there, for a project in our computer science lessons at school we decided to write a client/server based battleship like game . I know this game could be written without a server, but the...
1
by: Jerry Fleming | last post by:
Hi, I have wrote a game with python curses. The problem is that I want to confirm before quitting, while my implementation doesn't seem to work. Anyone can help me? #!/usr/bin/python # #...
0
by: Jeff Rush | last post by:
At PyCon this year we're going to have a multi-day game programming clinic and challenge. This is a first-time event and an experiment to find those in the Python community who enjoy playing and...
4
by: PatrickMinnesota | last post by:
I like to do fun stuff when learning a new language. I've been working with Python for a little while on real world problems mostly fixing bugs and writing a simulator for work. I was thinking...
10
by: Michael Lubker | last post by:
Any people that use Python as the predominant language for their game development here? ~Michael
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.