Problems using random.choice in a while loop
Question:
I’m trying to write a program that models the Prisoner’s Dilemma (for my own research purposes), and I’m having trouble generating a random computer choice for every round of the game. Here is my current while loop:
user_points = 0
N_points = 0
games_played = 0
while games_played != games:
print("Games played: ",games_played)
s = choice(["confess","remain silent"])
choice = input("Do you choose to confess to the crime or to remain silent? ")
if choice.lower() == "confess" or choice.lower() == "remain silent":
print("You chose to %s, and N chose to %s." % (choice,s))
else:
print("Please choose to either confess or remain silent.")
if choice.lower() == "confess" and s == "confess":
print("You have both confessed. You both get 9 years in prison.")
user_points -= 2
N_points -= 2
elif choice.lower() == "confess" and s == "remain silent":
print("You confessed while N remained silent. You walk free while N
spends the next 7 years in jail.")
N_points -= 4
elif choice.lower() == "remain silent" and s == "remain silent":
print("Fine then...Neither of you wants to talk, but you both still
get a year each for weapons charges.")
user_points += 1
N_points += 1
elif choice.lower() == "remain silent" and s == "confess":
print("You remained silent, but N confessed. You'll spend the next 7
years in jail while N walks free.")
user_points -= 4
games_played += 1
print("Your score is %s." % (user_points))
print("N's score is %s." % (N_points))
else:
print("Final Score: You scored %s points. N scored %s points." % (user_points,N_points))
if user_points > N_points:
return "Congrats! You have won the Prisoner's Dilemma."
elif user_points == N_points:
return "You and N tied. Neither of you wins the Prisoner's Dilemma."
else:
return "You have lost the Prisoner's Dilemma."
When I run the code, I get the following error:
Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>
prisonerDilemma()
File "/Users/stephenantonoplis/Documents/prisonersdilemma.py", line 167, in prisonerDilemma
s = choice(["confess","remain silent"])
TypeError: 'str' object is not callable
Does anyone know how I might fix this problem? I could define s outside of the while loop, but then the computer would use the same option for every round, which is not random.
Answers:
You’re assigning a string to choice
, which shadows the function definition it used to point to. Name it something else, or stick to random.choice
.
I’m trying to write a program that models the Prisoner’s Dilemma (for my own research purposes), and I’m having trouble generating a random computer choice for every round of the game. Here is my current while loop:
user_points = 0
N_points = 0
games_played = 0
while games_played != games:
print("Games played: ",games_played)
s = choice(["confess","remain silent"])
choice = input("Do you choose to confess to the crime or to remain silent? ")
if choice.lower() == "confess" or choice.lower() == "remain silent":
print("You chose to %s, and N chose to %s." % (choice,s))
else:
print("Please choose to either confess or remain silent.")
if choice.lower() == "confess" and s == "confess":
print("You have both confessed. You both get 9 years in prison.")
user_points -= 2
N_points -= 2
elif choice.lower() == "confess" and s == "remain silent":
print("You confessed while N remained silent. You walk free while N
spends the next 7 years in jail.")
N_points -= 4
elif choice.lower() == "remain silent" and s == "remain silent":
print("Fine then...Neither of you wants to talk, but you both still
get a year each for weapons charges.")
user_points += 1
N_points += 1
elif choice.lower() == "remain silent" and s == "confess":
print("You remained silent, but N confessed. You'll spend the next 7
years in jail while N walks free.")
user_points -= 4
games_played += 1
print("Your score is %s." % (user_points))
print("N's score is %s." % (N_points))
else:
print("Final Score: You scored %s points. N scored %s points." % (user_points,N_points))
if user_points > N_points:
return "Congrats! You have won the Prisoner's Dilemma."
elif user_points == N_points:
return "You and N tied. Neither of you wins the Prisoner's Dilemma."
else:
return "You have lost the Prisoner's Dilemma."
When I run the code, I get the following error:
Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>
prisonerDilemma()
File "/Users/stephenantonoplis/Documents/prisonersdilemma.py", line 167, in prisonerDilemma
s = choice(["confess","remain silent"])
TypeError: 'str' object is not callable
Does anyone know how I might fix this problem? I could define s outside of the while loop, but then the computer would use the same option for every round, which is not random.
You’re assigning a string to choice
, which shadows the function definition it used to point to. Name it something else, or stick to random.choice
.