could use some help with this problem! I've been working on it for days but cant seem to get it right !

breamoreboy at gmail.com breamoreboy at gmail.com
Tue Feb 20 00:13:21 EST 2018


On Tuesday, February 20, 2018 at 5:08:49 AM UTC, Marc Cohen wrote:
> USING PYTHON 2:
> 
> Write a program to play this game. This may seem tricky, so break it down into parts. Like many programs, we have to use nested loops (one loop inside another). In the outermost loop, we want to keep playing until we are out of stones.
> 
> Inside that, we want to keep alternating players. You have the option of either writing two blocks of code, or keeping a variable that tracks the current player. The second way is slightly trickier since we haven't learned lists yet, but it's definitely do-able!
> 
> Finally, we might want to have an innermost loop that checks if the user's input is valid. Is it a number? Is it a valid number (e.g. between 1 and 5)? Are there enough stones in the pile to take off this many? If any of these answers are no, we should tell the user and re-ask them the question
> 
> So, the basic outline of the program should be something like this:
> 
> totalStones = 100
> 
> maxStones = 5
> 
> pile = TOTAL # all stones are in the pile to start
> 
> while [pile is not empty]:
> 
> while [player 1's answer is not valid]:
> 
> [ask player 1]
> 
> [execute player1’s move]
> 
> Do the same for player 2…. (this can be achieved by a for loop)
> 
> Note how the important numbers 100 and 5 are stored in a single variable at the top. This is good practice -- it allows you to easily change the constants of a program. For example, for testing, you may want to start with only 15 or 20 stones.
> 
> Be careful with the validity checks. Specifically, we want to keep asking player 1 for their choice as long as their answer is not valid, BUT we want to make sure we ask them at least ONCE. So, for example, we will want to keep a variable which tracks whether their answer is valid, and set it to False initially.
> 
> Hint: the final number of stones can’t be negative. The game automatically stops when the number of stones reach zero.

Sorry old bean but we don't write code for you.  So you show us the code that you've been working on for days and tell us what's wrong with it, and we'll point you in the right direction.

--
Kindest regards.

Mark Lawrence.



More information about the Python-list mailing list