Im quite new to python. and I want to build a little programm for rock paper scissors lizard spok game. But I have a problem calling the fuction within itself with recursion. I always get the error "self not defined". Im not satisfied with my code but i think it will do the job i want it to do. If anyone could give me Feedback I would be very thankful.
from random import choice
class rock_paper_scissor_etc:
def play(results):
auswahl = ["Rock","Paper","Scissors","Lizard","Spok"]
computersChoice = choice(auswahl)
usersChoice = input()
if usersChoice == "Rock" :
if computersChoice == "Rock":
results.append("draw")
elif computersChoice == "Paper":
results.append("lose")
elif computersChoice == "Lizard":
results.append("win")
elif computersChoice == "Spok":
results.append("lose")
else:
results.append("win") #Scissors
if usersChoice == "Paper" :
if computersChoice == "Rock":
results.append("win")
elif computersChoice == "Paper":
results.append("draw")
elif computersChoice == "Lizard":
results.append("lose")
elif computersChoice == "Spok":
results.append("win")
else:
results.append("lose") #Scissors
if usersChoice == "Scissors" :
if computersChoice == "Rock":
results.append("lose")
elif computersChoice == "Paper":
results.append("win")
elif computersChoice == "Lizard":
results.append("win")
elif computersChoice == "Spok":
results.append("lose")
else:
results.append("draw") #Scissors
if usersChoice == "Lizard" :
if computersChoice == "Rock":
results.append("lose")
elif computersChoice == "Paper":
results.append("win")
elif computersChoice == "Lizard":
results.append("draw")
elif computersChoice == "Spok":
results.append("win")
else:
results.append("lose") #Scissors
if usersChoice == "Spok" :
if computersChoice == "Rock":
results.append("win")
elif computersChoice == "Paper":
results.append("lose")
elif computersChoice == "Lizard":
results.append("lose")
elif computersChoice == "Spok":
results.append("draw")
else:
results.append("win") #Scissors
print("User: " usersChoice)
print("Computer: " computersChoice)
print("play again?")
if input() == "yes":
print("test")
self.play(results)
else:
print(results)
results = []
play(results)
CodePudding user response:
self.play(results)
will not work, because there is no self
defined. self
is commonly used as the name of the first parameter of a method, but you have not included that parameter. Nor have you made the initial call of play
as a method call.
While you could fix that, there are some reasons why you should not pursue this programming pattern:
The recursion will become deeper and deeper for each player move. It makes more sense to create a loop, and exit that loop when the user does not want to play anymore.
The class is not really useful:
- You don't create an instance of it
- If you would, it would not carry any state
So create a plain function without a class
Other remarks / suggestions:
- Instead of requiring that the caller of
play
must provide an empty list, let the function return the list. - When you ask for input, provide a prompt as argument to
input
- The logic for determining the winner can be made a bit shorter:
- First check if both players had the same guess. Then you know it is a draw
- Then use the
in
operator to check whether there was a win - Test for all winning combinations in one expression (using
or
)
- You should check that the user has chosen a valid option and repeat asking for input until that is the case
- Maybe allow that the user enters a choice in a different capitalisation (all upper case or all lower case,...). So turn all input to lowercase, and also initialise the possible options with all-lowercase letters.
Here is code that takes the above into account:
from random import choice
def play():
results = []
auswahl = ["rock","paper","scissors","lizard","spok"]
while True:
computersChoice = choice(auswahl)
usersChoice = input("Choose " ", ".join(auswahl) ": ").lower()
while usersChoice not in auswahl:
usersChoice = input("Not a valid choice. Choose " ", ".join(auswahl) ": ").lower()
if usersChoice == computersChoice:
results.append("draw")
elif (usersChoice == "rock" and computersChoice in ("lizard", "scissors")
or usersChoice == "paper" and computersChoice in ("rock", "spok")
or usersChoice == "scissors" and computersChoice in ("paper", "lizard")
or usersChoice == "lizard" and computersChoice in ("paper", "spok")
or usersChoice == "spok" and computersChoice in ("rock", "scissors")):
results.append("win")
else:
results.append("lose")
print("User: " usersChoice)
print("Computer: " computersChoice)
if input("play again (yes, no)? ") != "yes":
print(results)
return results
results = play()