More Practice with Python
Rock Paper Scissors Game
The
#! python3
# rockpaperscissors.py
import random,logging
'------------------------------------------------------------------------------'
# Sets up logging
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s.%(msecs)03d: %(message)s', datefmt='%H:%M:%S')
logging.disable(logging.DEBUG) # uncomment to block debug logging.debug messages
logging.disable(logging.INFO) # uncomment to block debug logging.info messages
'------------------------------------------------------------------------------'
#Global variables
AI_WinTotal_int = 0 # holds running AI win count
playerWinTotal_int = 0 # holds running Player win count
'------------------------------------------------------------------------------'
def rockpaperscissors ():
# Initalize local variables
moveOptions_list = ['rock','paper','scissor']
AI_Choice_int = random.randint(0,len(moveOptions_list)-1)
AI_Move_str = moveOptions_list[AI_Choice_int]
logging.info('AI move: %s' % AI_Move_str)
playerMove_str = ''
winState = ''
'--------------------------------------------------------------------------'
#Game beginning state
print('choose your move:')
playerMove_str = input()
'--------------------------------------------------------------------------'
#Error Check
while playerMove_str.lower() not in moveOptions_list: # if player does not choose a valid move
print('ERROR: Invalid input must choose "rock", "paper" or "scissor"')
playerMove_str = input()
'--------------------------------------------------------------------------'
#game logic
if playerMove_str.lower() == 'rock':
if AI_Move_str == 'scissor' :
winState = 'WIN'
if AI_Move_str == 'paper' :
winState = 'LOSE'
if AI_Move_str == 'rock' :
winState = 'TIE'
if playerMove_str.lower() == 'paper':
if AI_Move_str == 'rock' :
winState = 'WIN'
if AI_Move_str == 'scissor' :
winState = 'LOSE'
if AI_Move_str == 'paper' :
winState = 'TIE'
if playerMove_str.lower() == 'scissor':
if AI_Move_str == 'paper' :
winState = 'WIN'
if AI_Move_str == 'rock' :
winState = 'LOSE'
if AI_Move_str == 'scissor' :
winState = 'TIE'
return winState, AI_Move_str , playerMove_str
'------------------------------------------------------------------------------'
# Outputs the results from the round and ask user if they want to continue playing
def endRound(winStateReturn, AI, Player):
#local variables
playAgain = True
userInput_PlayAgain = ''
'--------------------------------------------------------------------------'
# prints round resutls
print()
if winStateReturn == 'WIN':
print('WIN: You Win %s beats %s' % (Player, AI))
global playerWinTotal_int
playerWinTotal_int += 1
if winStateReturn == 'LOSE':
print('LOSE: You Lose %s beats %s' % (AI, Player) )
global AI_WinTotal_int
AI_WinTotal_int += 1
if winStateReturn == 'TIE':
print('TIE: You Tied %s equals %s' % (AI, Player))
print('Score - AI:%d Player:%d' % (AI_WinTotal_int,playerWinTotal_int)) # prints running score
'--------------------------------------------------------------------------'
# Determines if user wants to continue playing
print('\nWould you like to play again')
userInput_PlayAgain = input()
# Error Check
while userInput_PlayAgain != 'y' and userInput_PlayAgain != 'n':
print('ERROR: Invalid input please enter either "y" or "n".')
userInput_PlayAgain = input()
# logic determining if to play another round
if userInput_PlayAgain == 'y':
playAgain = True
if userInput_PlayAgain == 'n':
playAgain = False
return playAgain
'-------------------------------------------------------------------------------'
#main call
def main():
playState = True
print('This is a game of rock paper scissors')
while playState == True:
gameResult, AI_Play, Player_Play = rockpaperscissors()
playState = endRound(gameResult, AI_Play, Player_Play)
print('\nFinal Score - AI:%d Player:%d' % (AI_WinTotal_int,playerWinTotal_int)) # prints Final score
print('DONE: Exiting program')
if __name__ == '__main__':
main()