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 13022
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...
|
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...
|
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...
...
|
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...
|
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 =...
|
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...
|
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
#
#...
|
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...
|
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...
|
by: Michael Lubker |
last post by:
Any people that use Python as the predominant language for their game
development here?
~Michael
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
| |