Reputation: 43
I'm implementing a game which allows two players to pick alternately from a pile of 27 sticks. Each player may take 1, 2, or 3 sticks on each turn; the player forced to take the final stick loses.
I've done most of the code, but I must include validation. Besides taking 1-3 sticks, a player is not allowed to take the final stick. I've tried using a continue statement, but when player two exceeds the limit, the program returns to player 1's turn.
Here's what I have so far:
count = 27
T = False
F = False
while count > 1:
Playerone = int(input("Player 1's Turn, Enter from 1-3"))
if Playerone < 1 or Playerone > 3:
print("Error")
continue
count -= Playerone
print(count)
if count == 1:
print("P1 wins")
break
if count < 1:
print("You can't pick up those many sticks")
continue
Playertwo = int(input("Player 2's Turn, Enter from 1-3"))
if Playertwo < 1 or Playertwo > 3:
print("Error")
continue
count -= Playertwo
print(count)
if count == 1:
print("P2 wins")
break
if count < 1:
print("You can't pick up those many sticks")
continue
The last if statement is the issue
Help would be much appreciated,
Upvotes: 0
Views: 899
Reputation: 378
This is what I would do, you could go crazy with Classes
, but that is a little much for you right now(but something to look into). You should also look into creating methods, but check out my code below with the comments I left.
def player_input(player_number: int): # This is a method, that requires an integer to be pass to it
p_input = int(input("Player {}'s Turn, Enter from 1-3: ".format(player_number)))
while p_input < 1 or p_input > 3: # This will cause a loop continuously asking the player to input a number until that number is between 1 or 3.
print('Please choose a number between 1 and 3')
p_input = int(input("Player {}'s Turn, Enter from 1-3: ".format(player_number))) # Format will replace the '{}' with the value of the variable you give it
return p_input # This 'return' line will return the what the results of what the player typed in
def found_winner(stick_number: int): # stick_number is a required variable and ': int' requires that that variable be an integer
winner = False
if stick_number == 1:
winner = True
return winner # This method will return if a winner is found or not
def next_player(player_number: int): # This method will swap the players turn
if player_number == 1:
player_number = 2
elif player_number == 2:
player_number = 1
return player_number
def start_game(stick_count: int = 27): # This method will start the game, the '= 27' says that you can give me any stick count you want(that is an integer), but if you don't provide one I will use '27' by default
player_number = 1
while stick_count > 1: # This will loop through until the stick count is 1 or less
sticks_to_remove = player_input(player_number) # I store the plays result just in case the stick_count goes below 1, and then I remove the sticks if the the count doesn't go below 1
if stick_count - sticks_to_remove < 1:
print('You cant pick up that many sticks')
continue
else:
stick_count -= sticks_to_remove # Remove the sticks
if found_winner(stick_count): # Look for the winner
print('Player {} wins!'.format(player_number))
else:
player_number = next_player(player_number) # If no winner go to the next player
if __name__ == '__main__': # This says only execute the 'start_game()' method automatically if this python script is called, this is useful later when you start creating more complicated Python scripts that span multiple files.
start_game()
Upvotes: 0
Reputation: 77847
You have a basic flaw in your loop flow: regardless of the problem encountered with either player's input, you use continue
to return to the top of the loop, which gets you back to Player 1. You need to fix this: loop on a given player's input until it's valid in all ways. Something like this should do:
valid = False
while not valid:
Playertwo = int(input("Player 2's Turn, Enter from 1-3"))
if Playertwo < 1 or Playertwo > 3:
print("Error")
elif count - Playertwo < 1:
print("You can't pick up those many sticks")
else:
valid = True
Apply this to each player's input. Once you get out of this loop, you have valid input. From there, you can decrease the count and determine whether someone has won.
Upvotes: 1
Reputation: 30210
One way to ensure a valid user input is to use a loop.
Here's a quick example of a function you might use:
def prompt_integer(msg, minval, maxval, err_invalid, err_oob):
while True:
resp = input(msg) # Python3, use raw_input in Python2
try:
resp = int(resp)
if minval <= resp <= maxval:
return resp
else:
print(err_oob)
except ValueError:
print(err_invalid)
x = prompt_integer("Enter an integer: ", 1, 3, "Invalid Integer.", "Integer Out of Bounds")
Here, the function will not return until the user enters a valid integer between 1 and 3 (inclusive).
If they enter, say, 'abc', then the program will display "Invalid Integer." and ask them again.
If they enter, say, 5, when you've specified that the bounds are 1 and 3 then the program will display "Integer Out Of Bounds", and then ask them again.
When this function returns you know you've got an acceptable value.
You might use this function in your code, and modify maxval argument each time you call it according to how many sticks they're able to pick up.
Upvotes: 0