Python function arbitrary looping if statements

I make a scissor toy for stone paper and ran into a decisioncycle() problem. What I'm trying to do is ask the user to enter a choice in usercycle() , make the computer generate a random selection in gamecycle() , and then determine who won the round and track each result with the calculation of gain and loss. It seems to be a solution when need to work at random.

 import random class rpsgame: rps= ["rock", "paper","scissors"] wincount=0 losecount=0 def usercycle(self): userchoice = input("rock, paper, scissor.....") print("SHOOT") return userchoice def gamecycle(self): computerchoice = random.choice(rpsgame.rps) return computerchoice def decisioncycle(self): if rpsgame.usercycle(self) == rpsgame.rps[0] and rpsgame.gamecycle(self) == rpsgame.rps[1]: print("paper beats rock, you lose!") rpsgame.losecount +=1 elif rpsgame.usercycle(self) == rpsgame.rps[1] and rpsgame.gamecycle(self) == rpsgame.rps[0]: print("paper beats rock, you win!") rpsgame.wincount+=1 elif rpsgame.usercycle(self) == rpsgame.rps[0] and rpsgame.gamecycle(self) == rpsgame.rps[2]: print("rock beats scissors, you win!") rpsgame.wincount+=1 elif rpsgame.usercycle(self) == rpsgame.rps[2] and rpsgame.gamecycle(self) == rpsgame.rps[0]: print("rock beats scissors, you lose!") rpsgame.losecount+=1 elif rpsgame.usercycle(self) == rpsgame.rps[1] and rpsgame.gamecycle(self) == rpsgame.rps[2]: print("scissors beats paper, you lose!") rpsgame.losecount+=1 elif rpsgame.usercycle(self) == rpsgame.rps[2] and rpsgame.gamecycle(self) == rpsgame.rps[1]: print("scissors beats paper, you win!") rpsgame.wincount+=1 elif rpsgame.usercycle(self) == rpsgame.gamecycle(self): print("it a tie!!!") print("wins {}, losses {}".format(rpsgame.wincount, rpsgame.losecount)) while True: rg = rpsgame() rg.usercycle() rg.gamecycle() rg.decisioncycle() 

I think my problem is making a decision (). this is my first shot in class since the game works with global variables, but I read here that this is bad practice for the future.

+5
source share
5 answers

instead of evaluating each combination using multiple loops, you can use modular arithmetic.

lets say you do the mapping

 "rock" => 0 "paper"=>1 "scissors" => 2 

you can rate the solution as

 (A.number - B.number) % 3 

if this result is 0, it is a draw if it is 1, if won, if 2 A is lost

+3
source

You request a new user input in each condition. You probably want to read it only once, and then compare it every time, as

 user_choice = self.usercicle() game_choice = self.gamecycle() if(user_choice == self.rps[0] and game_choice == self.rps[1]): print "Paper beats rock, you lose!" self.losecount += 1 elif( user_choice... 

etc.

+2
source

I think you should make a separate function to determine the winner and use the dict statement, not the 7-way if:

 def who_won(player, computer): rules = {"rock": "scissors", "paper": "rock", "scissors": "paper"} if player == computer: return None if rules[computer] == player: return "computer" return "player" 

It is probably a good idea to check for invalid input, but this should be done in the input function, and not in this function.

+1
source

I think it really can be simplified. There are some errors, and they look as if they are related to simple unfamiliarity with classes. Look at here:

 class RpsGame: # create a game loop. Let try a while loop # also, let try using a dict to make comparisons easier def play(self): rps = {'rock':'scissors', 'paper':'rock', 'scissors':'paper'} score = 0 # lets just say player must win 3 or lose 3 to end the game while -3 < score < 3: # ask user for their choice just once here, for instance user = raw_input("Rock, paper or scissors: ").lower() # and check the input is valid # get the computer choice with random # then find the winner and adjust score # when score reaches -3, computer wins etc. # comparisons could go like: if com == rps[user]: score += 1 elif user == rps[com]: score -= 1 self.game_over(score) def game_over(self, score): if score == -3: result == "You win" else: result == "You lose" print "%s!!" % result # We would start by instantiating the game game = RpsGame() # And then calling the play method game.play() 

I would go and read about classes and using the "I" if you were you too.

+1
source

There are a lot of funky things here, but I think the problem you are facing is that you are passing the self function that does not accept the (really) argument as input.

 class TestClass(object): def some_method(self): return random.choice(['rock', 'paper', 'scissors']) def make_choice(self): return self.some_method(self) # raises TypeError 

The class of methods is AUTOMATICALLY defined by the instance to which it belongs as the first argument. If you try to transfer it again, it will not work.

However, I think your class should look something like this:

 class Roshambo(object): ROCK = 'rock' PAPER = 'paper' SCISSORS = 'scissors' OPTIONS = [ROCK, PAPER, SCISSORS] def get_user_input(self): choice = input("r/p/s? ").lower() if choice.startswith('r'): choice = self.ROCK elif choice.startswith('p'): choice = self.PAPER elif choice.startswith('s'): choice = self.SCISSORS else: # what do we do now? pass # for now return choice def get_computer_input(self): return random.choice(self.OPTIONS) def start(self): # get input ONCE... user_choice = get_user_input() computer_choice = get_computer_input() if user_choice == ROCK and computer_choice == ROCK: # tie # etc.... 

Then create an instance

 game = Roshambo() 

And run

 game.start() 
0
source

All Articles