Question: using python, fix any issues with this code: from random import randint print ( You have two choices ) print ( Option 1

using python, fix any issues with this code: from random import randint
print("You have two choices")
print("Option 1: Cooperate with the Authorities")
print("Option 2: Stay Silent")
partnerAchoice = int(input("Enter 1 or 2: "))
partnerBchoice = randint(1,2)
print(f"You chose {partnerAchoice}")
print(f"The computer chose {partnerBchoice}")
if (partnerAchoice ==1 and partnerBchoice ==1):
print("You both do 4 years in prison")
elif (partnerAchoice ==1 and partnerBchoice ==2):
print("You're released!!")
print("The computer does 6 years in prison")
elif (partnerAchoice ==2 and partnerBchoice ==1):
print("You do 6 years in prison")
print("The computer is released!")
elif (partnerAchoice ==2 and partnerBchoice ==2):
print("You both do 2 years in prison")
partnerAcount =0
partnerBcount =0
for i in range(5):
partnerAChoice = randint(1,2)
partnerBChoice = randint(1,2)
if partnerAChoice ==1 and partnerBChoice ==1:
partnerAcount +=4
partnerBcount +=4
elif partnerAChoice ==1 and partnerBChoice ==2:
partnerAcount +=0
partnerBcount +=6
elif partnerAChoice ==2 and partnerBChoice ==1:
partnerAcount +=6
partnerBcount +=0
else:
partnerAcount +=2
partnerBcount +=2
averageA = partnerAcount /5
averageB = partnerBcount /5
print('Partner A spent', partnerAcount, 'years in jail')
print('Partner B spent', partnerBcount, 'years in jail')
print('On average, Partner A spent', averageA, 'years in jail')
print('On average, Partner B spent', averageB, 'years in jail')
num_trials =0
while num_trials <10 or num_trials >500:
try:
num_trials = int(input('How many trials should be simulated? '))
if num_trials <10 or num_trials >500:
print('Please enter a number between 10 and 500')
except ValueError:
print('Invalid input. Please enter an integer.')
for i in range(num_trials):
partnerAChoice = randint(1,2)
partnerBChoice = randint(1,2)
averageA = partnerAcount / num_trials
averageB = partnerBcount / num_trials
print('Average for Partner A:', averageA)
print('Average for Partner B:', averageB)
partnerAstrategy =0
while partnerAstrategy <1 or partnerAstrategy >3:
partnerAstrategy = int(input('Select a strategy for Partner A (1-Random, 2-Only Silent, 3-Only Cooperates): '))
if partnerAstrategy <1 or partnerAstrategy >3:
print('Must enter a number between 1 and 3
Please try again')
partnerBstrategy =0
while partnerBstrategy <1 or partnerBstrategy >3:
partnerBstrategy = int(input('Select a strategy for Partner B (1-Random, 2-Only Silent, 3-Only Cooperates): '))
if partnerBstrategy <1 or partnerBstrategy >3:
print('Must enter a number between 1 and 3 Please try again')
num_trials =0
while num_trials <10 or num_trials >500:
num_trials = int(input('How many trials should be simulated? '))
if num_trials <10 or num_trials >500:
print('Please enter a number between 10 and 500')
for i in range(num_trials):
if partnerAstrategy ==1:
partnerAChoice = randint(1,2)
elif partnerAstrategy ==2:
partnerAChoice =2
else:
partnerAChoice =1
if partnerBstrategy ==1:
partnerBChoice = randint(1,2)
elif partnerBstrategy ==2:
partnerBChoice =2
else:
partnerBChoice =1
averageA = partnerAcount / num_trials
averageB = partnerBcount / num_trials
print('Partner A did an average of', averageA, 'years in prison')
print('Partner B did an average of', averageB, 'years in prison'),

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!