The error occurs in the def ‘newround()’, and the given options are Yes/No, however when entering something like ‘test’ it says the else statement but then gives an error anyways. I looked up some info on the error, and the best thing i could find was if a variable was called ‘str’ but none of my variables are even close to that so I’m not sure why I get this error, also I’m new to coding so apologies if it’s something simple. Here is the code:
JavaScript
x
227
227
1
# Importing the random module
2
import random
3
import time
4
5
# The title of the game is printed
6
print(""" ***************************************
7
Rock, Paper, Scissors! - By Max Pearson
8
***************************************""")
9
10
11
# All user defined functions apart from code() are defined here
12
13
# Defining the first Rock Paper Scissors
14
def rps1():
15
# Take the users input of RPS
16
user_choice = input("What is your choice? Rock, Paper, or Scissors?: ")
17
# Strips and lowers user input
18
user_choice = user_choice.lower()
19
user_choice = user_choice.strip()
20
RPS = ["Rock", "Paper", "Scissors"]
21
computer_names = ["Computer", "Robot", "Android", "A.I", "PC",
22
"Algorithm", "Laptop", "Alexa", "Windows"]
23
computer_name = random.choice(computer_names)
24
# Selecting the computer's choice
25
computer_choice = random.choice(RPS)
26
computer_choice = computer_choice.lower()
27
computer_choice = computer_choice.strip()
28
# Sets the parameters for when the user inputs Rock
29
if user_choice == "rock":
30
print("You have chosen Rock")
31
print("{} chose... {}".format(computer_name, computer_choice.title()))
32
if user_choice == computer_choice:
33
print("It's a tie!")
34
newround()
35
elif computer_choice == "paper":
36
print("You lose...")
37
newround()
38
else:
39
print("You win!")
40
newround()
41
# Sets the parameters for when the user inputs Paper
42
elif user_choice == "paper":
43
print("You have chosen Paper")
44
print("{} chose... {}".format(computer_name, computer_choice.title()))
45
if user_choice == computer_choice:
46
print("It's a tie!")
47
newround()
48
elif computer_choice == "rock":
49
print("You win!")
50
newround()
51
else:
52
print("You lose...")
53
newround()
54
# Sets the parameters for when the user inputs Scissors
55
elif user_choice == "scissors":
56
print("You have chosen Scissors")
57
print("{} chose... {}".format(computer_name, computer_choice.title()))
58
if user_choice == computer_choice:
59
print("It's a tie!")
60
newround()
61
elif computer_choice == "rock":
62
print("You lose...")
63
newround()
64
else:
65
print("You win!")
66
newround()
67
# Fallback for an invalid input
68
else:
69
print("Please enter a valid choice")
70
rps1()
71
72
73
# Defining the option for a new round
74
def newround():
75
# Take user input
76
playagain = input("Would you like to play another game?(Yes/No): ")
77
# Stripping and lowering the variable
78
playagain = playagain.strip()
79
playagain = playagain.lower()
80
try:
81
if playagain == "yes":
82
rps1()
83
elif playagain == "no":
84
print("Okay!")
85
else:
86
print("Please enter a valid input(Yes/No)")
87
playagain()
88
except ValueError:
89
print("Please enter a valid input(Yes/No)")
90
newround()
91
92
93
# Defining the function to turn numbers into percentages
94
def percentage(x, num_sims):
95
x = (x / num_sims) * 100
96
return x
97
98
99
# Gives the user the option to view statistics
100
def percentified(wins, losses, ties, num_sims):
101
# Take user input
102
percentages = input("Would you like these results in a statistic?: ")
103
percentages = percentages.lower()
104
percentages = percentages.strip()
105
if percentages == "yes":
106
# Printing and formatting the results
107
print(
108
"Here are the percentages to one decimal "
109
"point:nWins = {:.1f}%nLosses = {:.1f}%nTies = {:.1f}%".format(
110
percentage(wins, num_sims), percentage(losses, num_sims),
111
percentage(ties, num_sims)))
112
elif percentages == "no":
113
print("Okay, enjoy the results")
114
else:
115
print("Please enter a valid choice (Yes/No)")
116
percentified(wins, losses, ties, num_sims)
117
118
119
# The second gamemode of Rock Paper Scissors
120
def rps2():
121
# Defining a list for the random choice
122
RPS = ["Rock", "Paper", "Scissors"]
123
results = []
124
try:
125
# Takes an input from the user, to define the number of games
126
num_sims = int(input("Please enter the number of "
127
"simulated games you would like: "))
128
# Loops for the number the user entered
129
if num_sims > 0:
130
for i in range(0, num_sims):
131
choice1 = random.choice(RPS)
132
choice2 = random.choice(RPS)
133
# Runs a check on every choice and adds it to a list
134
if choice1 == choice2:
135
results.append("tie")
136
elif choice1 == "Rock" and choice2 == "Paper":
137
results.append("loss")
138
elif choice1 == "Rock" and choice2 == "Scissors":
139
results.append("win")
140
elif choice1 == "Scissors" and choice2 == "Paper":
141
results.append("win")
142
elif choice1 == "Scissors" and choice2 == "Rock":
143
results.append("loss")
144
elif choice1 == "Paper" and choice2 == "Rock":
145
results.append("win")
146
elif choice1 == "Paper" and choice2 == "Scissors":
147
results.append("loss")
148
else:
149
print("Please enter a valid choice")
150
rps2()
151
# Count the results and store them in a variable
152
wins = results.count("win")
153
losses = results.count("loss")
154
ties = results.count("tie")
155
# Print the user their results
156
print("Here are the results:nWins = {}n"
157
"Losses = {}nTies = {}".format(wins, losses, ties))
158
percentified(wins, losses, ties, num_sims)
159
else:
160
print("Please enter a valid number above 0")
161
rps2()
162
# Fallback incase user enters a string to the integer input
163
except ValueError:
164
print("Please enter a valid number")
165
rps2()
166
return wins, losses, ties, num_sims
167
168
169
def rps3():
170
RPS = ["rock", "paper", "scissors"]
171
user_choice = input("Please enter a choice (Rock, Paper, Scissors): ")
172
user_choice.lower()
173
user_choice.strip()
174
computer_choice = random.choice(RPS)
175
if user_choice == computer_choice:
176
print("Congratulations! You got it correct!")
177
newround2()
178
elif user_choice == "rock" or
179
user_choice == "paper" or user_choice == "scissors":
180
print("Oof, that was the wrong choice, better luck next time!")
181
newround2()
182
else:
183
print("Please enter a valid choice (Rock, Paper, Scissors): ")
184
rps3()
185
186
187
def newround2():
188
playagain = input("Would you like to play again (Yes/No)?: ")
189
playagain.lower()
190
playagain.strip()
191
if playagain == "yes":
192
rps3()
193
elif playagain == "no":
194
print("Okay!")
195
else:
196
print("Please enter a valid choice (Yes/No: ")
197
newround2()
198
199
200
try:
201
# Defining the entirety of the body of the code
202
def code():
203
time.sleep(0.5)
204
# Takes the users input
205
try:
206
playstyle = int(input("Would you like to play RPS, simulate "
207
"a number of games, or guess "
208
"the computer's choice? (1,2,3): "))
209
except ValueError:
210
print("Please enter a valid choice")
211
code()
212
if playstyle == 1:
213
rps1()
214
# Checks if the user wants to simulate games
215
elif playstyle == 2:
216
rps2()
217
elif playstyle == 3:
218
rps3()
219
else:
220
print("Please enter a valid choice (1/2/3)")
221
code()
222
code()
223
# Fallback incase user enters a string to the integer input
224
except ValueError:
225
print("Please enter a valid choice (1/2/3)")
226
code()
227
Here is the error message:
JavaScript
1
11
11
1
Traceback (most recent call last):
2
File "/Users/maxp/PycharmProjects/pythonProject/RPS_maxpearson.py", line 222, in <module>
3
code()
4
File "/Users/maxp/PycharmProjects/pythonProject/RPS_maxpearson.py", line 213, in code
5
rps1()
6
File "/Users/maxp/PycharmProjects/pythonProject/RPS_maxpearson.py", line 40, in rps1
7
newround()
8
File "/Users/maxp/PycharmProjects/pythonProject/RPS_maxpearson.py", line 87, in newround
9
playagain()
10
TypeError: 'str' object is not callable
11
Advertisement
Answer
In def newround
, you have a variable called playagain
, but inside the except
part, you use it as a function. Maybe, rename your variables such that there would be no interference variables and function names.