Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This code worked the seemed to work the first time I ran it in python idle but now its getting an error half the time

This code worked the seemed to work the first time I ran it in python idle but now its getting an error half the time what needs to change to get this code working correctly all the time? This is the original instructions and below that is the code Write a program in which the computer plays against a human opponent. Generate a random integer between 10 and 100 to denote the initial size of the pile. Generate a random integer between 0 and 1 to decide wheter the computer or the human takes the first turn. Generate a random integer between 0 and 1 to decide whether the computer plays smart or stupid. In stupid mide the computer simply takes a random legal value (between 1 and n/2) from the pile whenever it has a turn. In smart mode the computer takes off enough marbles to make the size of the pile a power of two minus 1 - that is, 3,7,15,31, or 63. That is always a legal move, except when the zise of the pile is currently one less than a power of two. In that casem the computer makes a random legal move. Obviously the computer cannot be beaten in smart mode when it has the first move unless the pile size happeneds to be 15,31, or 63. And if the human has the first turn and knows the winning strategy they can win against the computer. def main(): 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:" + str(numOfMarbles)) # 1st CORRECTION  # Generate a random integer between 0 and 1 to decide wheter the computer or the human takes the first turn.  turn = random.randint(0, 1) if turn == 0: print("Its the turn of a computer") else: print("Its your turn") # Generate a random integer between 0 and 1 to decide whether the computer plays smart or stupid.  stupidComputer = random.randint(0, 1) if stupidComputer == 0: print("Vs Smart computer") else: print("Vs Dumb computer") # In stupid mode the computer simply takes a random legal value (between 1 and n/2)  # from the pile whenever it has a turn.  while numOfMarbles > 1: # Till you're left with only one marble. #2nd Correction  if turn == 0: # Computer turn  if stupidComputer == 1: # If the computer is playing in stupid mode.  temp = random.randint(1, numOfMarbles / 2) # Select a random number between 1, and n/2.  else: # Select the marbles smartly.  if numOfMarbles - 3 > 0 and numOfMarbles - 3 < int(numOfMarbles / 2): temp = numOfMarbles - 3 elif numOfMarbles - 7 > 0 and numOfMarbles - 7 < int(numOfMarbles / 2): temp = numOfMarbles - 7 elif numOfMarbles - 15 > 0 and numOfMarbles - 15 < int(numOfMarbles / 2): temp = numOfMarbles - 15 elif numOfMarbles - 31 > 0 and numOfMarbles - 31 < int(numOfMarbles / 2): temp = numOfMarbles - 31 else: temp = numOfMarbles - 63 print("The computer picked: %d marbles." % temp) # Print the marbles picked by computer.  numOfMarbles =numOfMarbles - temp # Subtract the marbles picked by computer.  else: marblesToPick = int(input("Its your turn. Pick the marbles in the range 1 - " + str(int(numOfMarbles / 2)) + ":")) # Read the marbles to be picked.  #5th Correction  #the validation condition was implemented wrong  #please find the new one below  while 1: # If read invalid value, try repeatedly.  if marblesToPick < 1 or marblesToPick > int(numOfMarbles / 2): marblesToPick = input("You picked the wrong count. Pick the marbles in the range 1 - %d: ") % int(numOfMarbles / 2) else: break  numOfMarbles = numOfMarbles - marblesToPick # Subtract the marbles picked by you.   turn = (turn + 1) % 2 # Change the turn. //3rd Correction  print("the pile is of size + " + str(numOfMarbles) + " .")#4th Correction START indentation problem this block is expected outside the while  if turn == 0: # Declare the result.  print("You won.") else: print("The computer won") #4th Correction END main()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design Using Entity Relationship Diagrams

Authors: Sikha Saha Bagui, Richard Walsh Earp

3rd Edition

103201718X, 978-1032017181

More Books

Students also viewed these Databases questions