1,105,384 Community Members

Rock Paper Scissors and how to break out

Member Avatar
hughesadam_87
Posting Whiz in Training
274 posts since May 2009
Reputation Points: 54 [?]
Q&As Helped to Solve: 13 [?]
Skill Endorsements: 1 [?]
 
0
 

Hey everyone,

I wrote a code which runs a game of rock paper scissors, and each time you lose, it counts down until at 0, it exits. The only way I've been able to exit the code is to throw in a 'sys.exit()' command. I hear there are better ways to exit, but don't understand them. Especially the use of the 'main' function, which is not well-explained in many web sources.

I am going to post the code below. If you can think of a better way to end, please let me know. Also, because I am new to coding and especially to Object Oriented Programming, if you see smarter ways to do anything in the code, please point them out. All suggestions are appreciated.

#!/usr/bin/python
#ROCK PAPER SCISSORS WITH CLASSES FOR PRACTICE

import re
import random
from numpy import *

condition = 'true'

class RockPaperScissors:

	'''Rock paper scissors game, best of 3'''

	def __init__(self, tries, points):
		self.tries = tries
		self.points = points
		self.entry = str(raw_input('Enter rock, paper, scissors (NO CAPS)' '\n'))
		'Reads in users call'
		print '---------------------' '\n' 'You picked %s' % self.entry  

		
	def random(self):
		'Generates a random number'
		number = random.randint(1, 3000)       
		'Test if even or odd'
		if number <= 10:
			output = 'paper'
		elif number < 20:
			output = 'scissors'
		else:
			output = 'rock'
		print 'Computer draws', output	
		return output                      
	


	def decision(self, rand_output):
		'Decides if win, draw or loss'
		self.rand_output = rand_output  
		if rand_output == self.entry:    
			print 'A draw!'
			self.tries += 0
			status = 'no'		
		
		elif rand_output == 'rock' and self.entry == 'paper':
			print 'You Win'
			self.tries += 1
			status = 'yes'
				
		
		elif rand_output == 'rock' and self.entry == 'scissors':
			print 'You Lose'
			self.tries -= 1
			status = 'no'		

		elif rand_output == 'paper' and self.entry == 'rock':
			print 'You Lose'
			self.tries -= 1
			status = 'no'

		elif rand_output == 'paper' and self.entry == 'scissors':
			print 'You Win'
			self.tries += 1
			status = 'no'

		elif rand_output == 'scissors' and self.entry == 'paper':
			print 'You Lose'
			self.tries -= 1
			status = 'no'

		elif rand_output == 'scissors' and self.entry == 'rock':
			print 'You Win'
			self.tries += 1
			status = 'yes'
		
		else:
			print 'YOU DIDNT ENTER ROCK PAPER OR SCISSORS, GENIUS'    

		print 'Tries left =', self.tries	
		
		return status


	def score(self, dec_output):
		self.dec_output = dec_output
		'Assigns points based on win or loss'				
		if dec_output == 'yes':
			self.points += 100
		else:
			self.points += 0

		print 'You earned', self.points, 'points'

		newscore = self.points

		return newscore


	def exit(self):
		if self.tries == 0:
			condition = 'false'		
			print 'GAME OVER-'
			sys.exit()

		remaining_tries = self.tries

		return remaining_tries
	
	


		
	
load = RockPaperScissors(3, 0)   	
#3 is the number of tries to star with, 0 is the number of points
output_rand = load.random()             
output_decision = load.decision(output_rand)         
output_score =load.score(output_decision)            
output_exit = load.exit()


while condition == 'true':
	print '--------------------------' '\n'
	load = RockPaperScissors(output_exit, output_score)
	output_rand = load.random()             
	output_decision = load.decision(output_rand)            
	output_score =load.score(output_decision)  
	output_exit = load.exit()
Member Avatar
Paul Thompson
Veteran Poster
1,095 posts since May 2008
Reputation Points: 178 [?]
Q&As Helped to Solve: 189 [?]
Skill Endorsements: 3 [?]
 
0
 

Yeah, there is another way

def exit(self):
		if self.tries == 0:
			condition = 'false'		
			print 'GAME OVER-'
			return False

		remaining_tries = self.tries
                return remaining_tries

Then just put this at the end of the Main function

if output_exit == False:
    return

That way, when it returns false the main function exits. The program will then end because there is no code left :)

Hope that helps.

Member Avatar
hughesadam_87
Posting Whiz in Training
274 posts since May 2009
Reputation Points: 54 [?]
Q&As Helped to Solve: 13 [?]
Skill Endorsements: 1 [?]
 
0
 

Thanks for the input but I"m still a bit confused. Should I define a main function then? If so, what exactly do I need?

Member Avatar
Paul Thompson
Veteran Poster
1,095 posts since May 2008
Reputation Points: 178 [?]
Q&As Helped to Solve: 189 [?]
Skill Endorsements: 3 [?]
 
1
 

I would basically put all of this in the main function.

load = RockPaperScissors(3, 0)   	
#3 is the number of tries to star with, 0 is the number of points
output_rand = load.random()             
output_decision = load.decision(output_rand)         
output_score =load.score(output_decision)            
output_exit = load.exit()


while condition == 'true':
	print '--------------------------' '\n'
	load = RockPaperScissors(output_exit, output_score)
	output_rand = load.random()             
	output_decision = load.decision(output_rand)            
	output_score =load.score(output_decision)  
	output_exit = load.exit()

So then it will look like this:

def main()
    load = RockPaperScissors(3, 0)   	
    #3 is the number of tries to star with, 0 is the number of points
    output_rand = load.random()             
    output_decision = load.decision(output_rand)         
    output_score =load.score(output_decision)            
    output_exit = load.exit()


    while condition == 'true':
            print '--------------------------' '\n'
            load = RockPaperScissors(output_exit, output_score)
            output_rand = load.random()             
            output_decision = load.decision(output_rand)            
            output_score =load.score(output_decision)  
            output_exit = load.exit()

And then we can add in out exit condition.

def main()
    load = RockPaperScissors(3, 0)   	
    #3 is the number of tries to star with, 0 is the number of points
    output_rand = load.random()             
    output_decision = load.decision(output_rand)         
    output_score =load.score(output_decision)            
    output_exit = load.exit()


    while condition == 'true':
            print '--------------------------' '\n'
            load = RockPaperScissors(output_exit, output_score)
            output_rand = load.random()             
            output_decision = load.decision(output_rand)            
            output_score =load.score(output_decision)  
            output_exit = load.exit()
            if output_exit == False:
                return

And that will work if you have changed the exit function to the one i outlined above.

Hope that helps:)

Member Avatar
hughesadam_87
Posting Whiz in Training
274 posts since May 2009
Reputation Points: 54 [?]
Q&As Helped to Solve: 13 [?]
Skill Endorsements: 1 [?]
 
0
 

Certainly helps, thanks alot.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article