473,738 Members | 8,397 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Nim game for python

1 New Member
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 13088
Subsciber123
87 New Member
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
87 New Member
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
2710
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 that a game The Temple Of Elemental Evil is going to be using python as its scripting language. It says that on the FAQ which you can read about here http://mywebpages.comcast.net/ibnobody/troika.html Q. What language are you writing the gamecode...
1
2492
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 (www.andromedaonline.net), goes
4
3789
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... Initially I was justing looking at using Python for some event scripting. So basically an event would trigger an object to run the appropriate Python script, which would be run in it's entirety and return control to the C++ code. After looking...
4
2718
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 I have found, and the only thing that makes me think it might be possible is this: http://asbahr.com/python.html I would be interested to hear anybody's experience with Python on consoles.
6
1661
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 = random.randint(1, 100) b=-100 c=0 print "Welcome to guess the number"
0
2177
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 whole project is for educational purposes. Being the initiator of this project, I thought I would write a skeleton for it before we actually start with it. The client thing won't be too hard, what I'm having problems with is the server, maybe...
1
3694
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 # # Brick & Ball in Python # by Jerry Fleming <jerryfleming@etang.com>
0
1837
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 creating games. Python has several powerful modules for the creation of games among which are PyGame and PyOpenGL. On Friday evening, Phil Hassey will give an introduction to his game Galcon, an awesome high-paced multi-player galactic...
4
3149
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 as a hobby project developing a simple 2d game (think Donkey Kong, Space Invaders, LoadRunner) and mentioned this to my 14 year old nephew. About a week later I got a full write up for a game that he wrote and would like to play. So I figure why...
10
2017
by: Michael Lubker | last post by:
Any people that use Python as the predominant language for their game development here? ~Michael
0
8969
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8788
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9476
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9335
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9263
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6751
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6053
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4825
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2745
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.