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 ")
2 13088
First of all, use code tags like this: - 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")
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: - print "Welcome to Nim."
-
sticks=13
-
over=False
-
while not over: # while the game is not over
-
pick=0
-
while not 1<=pick and not pick<=4 and not sticks<pick:
-
# while your conditions are not satisfied
-
try:
-
pick=int(raw_input("How many sticks would you like to pick? "))
-
except ValueError: # if the user doesn't enter a number, int() raises ValueError
-
pass
-
sticks-=pick # subtract pick from sticks
-
print "You have %d sticks remaining."%sticks # formatted string
-
if sticks==0:
-
over=True # could also use "break" instead of this flag
-
raw_input("You win!")
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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
|
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...
|
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.
|
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"
| |
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...
|
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>
|
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...
|
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...
|
by: Michael Lubker |
last post by:
Any people that use Python as the predominant language for their game
development here?
~Michael
|
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...
| |
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,...
|
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...
|
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...
|
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,...
|
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...
|
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();...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |