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