Reputation: 21
I am trying to insert code at the end of my game to either restart or end the game. I'm not quite sure what I'm missing here.
This is where I'm at:
print("NIM: The Python Game")
print("Hello! My name is AI (the clever computer). \nIn this game, 2 other players will play against me and each other. \nThe player to go first will randomly choose between 30 and 50 stones in a pile to start the game.")
print("She\He will also be the first to remove the first stones. \nPlayers will then take turns at removing between 1 and 3 stones from the pile until a player removes the final stone. \nThe player to remove the final stone is the winner.")
player1=str(input("\nPlayer 1, what is your name and MIT ID? "))
print("Hello, " + player1.capitalize())
player2=str(input("\nPlayer 2, what is your name and MIT ID? "))
print("Hello, " + player2.capitalize())
player3="The computer"
howMany=0
stonesNumber=0
while True:
stonesNumber=int(input("\nNEW GAME \nHow many stones do you want to start with (between 30 and 50), " + player1.capitalize() + "? "))
if stonesNumber <30:
print("The number must be between 30 and 50!")
elif stonesNumber >50:
print("The number must be between 30 and 50!")
else:
print("The number of stones is ", stonesNumber)
while True:
print("\nIt's your turn, ",player1.capitalize())
while True:
howMany=int(input("How many stones do you want to remove?(from 1 to 3) "))
if howMany in [1,2,3]:
break
print("Enter a number between 1 and 3.")
while howMany>stonesNumber:
print("The entered number is greater than a number of stones remaining.")
howMany=int(input("How many stones do you want to remove?"))
stonesNumber=stonesNumber-(howMany)
print("Numbers of stones left: " + str(stonesNumber))
if stonesNumber==0:
print(player1.capitalize()," wins.")
break
print("\nIt's your turn, ",player2.capitalize())
while True:
howMany=int(input("How many stones do you want to remove?(from 1 to 3) "))
if howMany in [1,2,3]:
break
print("Enter a number between 1 and 3.")
while howMany>stonesNumber:
print("The entered number is greater than a number of stones remaining.")
howMany=int(input("How many stones do you want to remove?"))
stonesNumber=stonesNumber-(howMany)
if stonesNumber==0:
print(player2.capitalize()," wins.")
break
print("Numbers of stones left: " + str(stonesNumber))
print("\nIt's my turn!")
if stonesNumber % 3==0:
howMany=2
else:
howMany=1
stonesNumber=stonesNumber-(howMany)
if howMany == 1:
print("The computer has taken 1 counter")
if howMany == 2:
print("The computer has taken 2 counters")
if stonesNumber==0:
print(player3," wins.")
break
print("Numbers of stones left: " + str(stonesNumber))
print("\nThe game is over!")
answer=str(input("\nDo you want to play again?(y/n)"))
if answer == "y" or "yes":
print("\nRestarting the game")
else:
break
print("\nThanks for playing the game!")
Upvotes: 2
Views: 154
Reputation: 677
I am assuming the problem you are having now is that you cannot escape the loop regardless of what you answer at the end of the game.
The reason your game can never escape the while loop is because of this line
if answer == "y" or "yes":
Your above if statement is made up of two parts:
if answer == "y"
(Evaluates to true or false depending on answer)if "yes"
(Evaluates to true every time)Since its an OR case, where either the result will be true/false OR true, you will always end up with if answer == "y" or "yes"
as true.
Therefore, you will always be stuck in the loop.
As per felix's answer, your solution would be to evaluate if answer == "y" OR answer == "yes"
instead
Upvotes: 2
Reputation: 82
The condition if answer == "y" or "yes":
does NOT check if answer equals "y" or equals "yes". Due to semantics of OR-Operator it "transposes" to a piece of code like if "yes":
, which is always true. Therefore it will always restart your game.
You will need to change it to that:
if answer == "y" or answer == "yes":
Upvotes: 2