1.11M Members

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'))
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

#3 is the number of tries to star with, 0 is the number of points

while condition == 'true':
print '--------------------------' '\n'

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.

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?

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

while condition == 'true':
print '--------------------------' '\n'

So then it will look like this:

``````def main()
#3 is the number of tries to star with, 0 is the number of points

while condition == 'true':
print '--------------------------' '\n'

And then we can add in out exit condition.

``````def main()
#3 is the number of tries to star with, 0 is the number of points

while condition == 'true':
print '--------------------------' '\n'
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:)

Certainly helps, thanks alot.

You