I need to fix my invalid syntax problem, but I also need to have a way to end th
ID: 3748402 • Letter: I
Question
I need to fix my invalid syntax problem, but I also need to have a way to end the game and display whether the computer or player won.
4 created on sat sep 15 ug:54:e2 2 18 Name Type pile_int int pile one int pile str str pile tWO int start_int int start str str stone_num int stone str str Si2e Value 6 @author: chrissteers 9 print("Welcome to the gane of Nim! I'm probably going to win..") 10 print("Nim is a simple two-player gane based on removing stones." 11 print("The gane begins with two piles of stones, numbered 1 and 2.") 12 print("Players alternate turns. Each turn, a player chooses to renove one,") 13 print("two, or three stones from sone single pile. The player who removes the") 14 print"last stone wins the game.") 15 start str-input "Would you like to play? (0-no, 1-yes) ) 16 start int- intistart str) 17 if start_int--0 18 print("Thanks for playing! See you again soon!") 19 elif startint=1: 20 print "Start-- Pile 1: 5 Pile 2: 5" 21 pile one- int (5) 22 pile two- int (5) 23 pile stra input"Choose a pile (1 or 2): ") 24 pile intint(pile str) exit - Variable explorer File explorer 25 while pile_one and pile two>e pile-int#1 : Python censale 26 27 2B 29 30 eli pile int-2: 31 32 if stone_str input( "Choose stones to remove from pile:) stone_num-int (stone str) pile one stone_num Console 1/A exec code obj, self.user global_ns, self.user_ns) stone str input"Choose stones to remove fron pile: ") stone_num intlstone str) pile twostone_num File "ipython-input-18-778de5838118", ine 1, in Renove", stonenum,"stones from pile",pile int,) 35 print"Pile 1: ",pile one,"Pile 2: ",pile two,) 36 37 if pile-int=1 and pile-tw029: File "/Users/chrissteers/anaconda3/11b/python3.6/site-packages/spyder/utils/ pile two 1 site/sitecustomize.py", ine 785, in runtile exectile(ilenare, namespace) 38 elif pile_int-2 and pile_one: 39 40 pile-int- { pile-int % 2} +1 # opposite to player 41 print"Conputer Remove 1 stones from pile".pile_intl 42 print "Pile 1: ".pile one,"Pile 2: ",pile two,) 43 pile-str= input("Choose a pile (1 or 2); " 44 pile_int- int pile_str) 45 if pile one0: 46 47 48 elif pile two9: 49 50 51 52 pile one 1 File "users/chrissteers/anaconda3/1ib/python3.6/site-packages/spyder/utis site/sitecustomize.py", line 182, in exectile exec compile(f.read), filename, exec, nanespace) File "/Users/chrissteers/CSE 231/proj 02.", line 25 while pite one and pite two e pile str input("Choose a pile 1 or 2): ) pile int int pile_str) SyntaxError: invalid syntax pile str input("Choose a pile 1 or 2): ") pile int int(pile str) In (111: In [111: Python console History logExplanation / Answer
Add : (colon) after while loop to avoid invalid syntax error. Write the while loop as follows
while pile_one and pile_two > 0:
You should add a : (colon) as above
In order to end the game and display whether the player or the computer won the game make the following changes in your code
print("Player -> Remove", stone_num,"stones from pile",pile_int,)
print("Pile 1 : ",pile_one,"Pile 2 : ",pile_two,)
if pile_one == 0 or pile_two == 0:
print("Player won the game")
else:
if pile_int == 1 and pile_two > 0:
pile_two -= 1
elif pile_int == 2 and pile_one > 0:
pile_one -= 1
pile_int = (pile_int%2)+1
print("Computer -> Remove 1 stones from pile",pile_int)
print("Pile 1 : ",pile_one,"Pile 2 : ",pile_two,)
if pile_one == 0 or pile_two == 0:
print("Computer won the game")
else:
pile_str = input("Choose a pile (1 or 2) : ")
pile_int = int(pile_str)
if pile_one == 0:
pile_str = input("Choose a pile (1 or 2) : ")
pile_int = int(pile_str)
elif pile_two == 0:
pile_str = input("Choose a pile (1 or 2) : ")
pile_int = int(pile_str)