Need help fixing this code in Python. It does not run correctly. Also need help
ID: 3682720 • Letter: N
Question
Need help fixing this code in Python. It does not run correctly.
Also need help adding in the computers input when it is the computers turn. If the human does not get to go first I need to add in the computers side of its turn.
import random
#Generate a random integer between 10 and 100 to denote the initial size of the pile.
numOfmarbles = random.randint(10,100)
print("The size of the pile is: ", numOfmarbles)
#Generate a random integer between 0 and 1 to decide whether the computer or the player takes the first turn.
turn = random.randint(0, 1)
if turn == 0:
print("Its the computers turn...")
else:
print("Its your turn...")
#Generate a random integer between 0 and 1 to decide whether the computer plays in stupid or smart mode.
stupidComputer = random.randint(0, 1)
if stupidComputer == 0:
print("The computer is playing in smart mode...")
else:
print("The computer is playing in stupid mode...")
#In stupid mode the computer simple takes a random legal value (between 1 and n/2) from the pile whenever it has a turn.
while numOfmarbles != 1:
if turn == 0:
if stupidComputer == 1:
temp = random.randint(1, numOfmarbles/2)
elif numOfmarbles - 3 > 0 and numOfmarbles - 3 < numOfmarbles/2:
temp = numOfmarbles - 3
elif numOfmarbles - 7 > 0 and numOfmarbles - 7 < numOfmarbles/2:
temp = numOfmarbles - 7
elif numOfmarbles - 15 > 0 and numOfmarbles - 15 < numOfmarbles/2:
temp = numOfmarbles - 15
elif numOfmarbles - 31 > 0 and numOfmarbles - 31 < numOfmarbles/2:
temp = numOfmarbles - 31
else:
temp = numOfmarbles - 63
print("The computer picked: %d marbles.") % temp
numOfmarbles -= temp
else:
marblesTopick = int(input("Its your turn. Pick the marbles in the range 1 - %d: " % int(numOfmarbles/2)))
while marblesTopick < 1 or marblesTopick > numOfmarbles/2:
marblesTopick = int(input("You picked the wrong count of marbles. Pick the marbles in the range 1 - %d: " % int(numOfmarbles/2)))
numOfmarbles -= marblesTopick
turn = turn( + 1) % 2
print("Now the pile os of size %d. ") % numOfmarbles
if turn == 0:
print("You Won!!!")
else:
print("You Lost!")
Getting a error on the line- turn = turn( + 1) % 2.
Please run the program before you post a solution. Have had numerous people try and none of them have worked. Thanks!
Explanation / Answer
import random
#Generate a random integer between 10 and 100 to denote the initial size of the pile.
numOfmarbles = random.randint(10,100)
print("The size of the pile is: ", numOfmarbles)
#Generate a random integer between 0 and 1 to decide whether the computer or the player takes the first turn.
turn = random.randint(0, 1)
if turn == 0:
print("Its the computers turn...")
else:
print("Its your turn...")
#Generate a random integer between 0 and 1 to decide whether the computer plays in stupid or smart mode.
stupidComputer = random.randint(0, 1)
if stupidComputer == 0:
print("The computer is playing in smart mode...")
else:
print("The computer is playing in stupid mode...")
#In stupid mode the computer simple takes a random legal value (between 1 and n/2) from the pile whenever it has a turn.
while numOfmarbles != 1:
if turn == 0:
if stupidComputer == 1:
temp = random.randint(1, numOfmarbles/2)
elif numOfmarbles - 1 > 0 and numOfmarbles - 1 <= numOfmarbles/2:
temp = numOfmarbles - 1
elif numOfmarbles - 3 > 0 and numOfmarbles - 3 <= numOfmarbles/2:
temp = numOfmarbles - 3
elif numOfmarbles - 7 > 0 and numOfmarbles - 7 <= numOfmarbles/2:
temp = numOfmarbles - 7
elif numOfmarbles - 15 > 0 and numOfmarbles - 15 <= numOfmarbles/2:
temp = numOfmarbles - 15
elif numOfmarbles - 31 > 0 and numOfmarbles - 31 <= numOfmarbles/2:
temp = numOfmarbles - 31
else:
temp = numOfmarbles - 63
print("The computer picked: %d marbles.") % temp
numOfmarbles -= temp
else:
marblesTopick = int(input("Its your turn. Pick the marbles in the range 1 - %d: " % int(numOfmarbles/2)))
while marblesTopick < 1 or marblesTopick > numOfmarbles/2:
marblesTopick = int(input("You picked the wrong count of marbles. Pick the marbles in the range 1 - %d: " % int(numOfmarbles/2)))
numOfmarbles -= marblesTopick
turn = (turn+1) % 2
print("Now the pile os of size %d. ") % numOfmarbles
if turn == 0:
print("You Won!!!")
else:
print("You Lost!")
#Here is the link to dropbox of code with correct indentation https://www.dropbox.com/s/nunv1g4tv7nebmk/chegg.pdf?dl=0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.