\documentclass[openany, 12pt]{book} 

\usepackage{color}
\usepackage{xcolor} 
\usepackage[skip=2pt,font=footnotesize]{subcaption}
\usepackage{listings}
\usepackage{xepersian}
%---listings colors 
\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.58,0,0.82}
\definecolor{backcolour}{rgb}{0.95,0.95,0.92}
%---listings colors 
\lstdefinestyle{mystyle}{
	tabsize=4,
	breaklines=true,
	breakatwhitespace=true,
	basicstyle=\footnotesize\ttfamily,
	numberstyle=\footnotesize\ttfamily,
	aboveskip=\baselineskip,
	columns=fullflexible,
	showstringspaces=false,
	extendedchars=true,
	breaklines=true,
	showtabs=false,
	showspaces=false,
	showstringspaces=false,
	identifierstyle=\ttfamily,
    %backgroundcolor=\color{backcolour},   
    commentstyle=\scriptsize\ttfamily\itshape,
    numberstyle=\tiny\color{black},
    stringstyle=\bfseries\ttfamily,
    basicstyle=\LSTfont,
    breakatwhitespace=false,         
    breaklines=true,                 
    captionpos=t,                    
    keepspaces=true,                 
    numbers=left,                    
    numbersep=5pt,                  
    showspaces=false,                
    showstringspaces=false,
    showtabs=false,                  
    tabsize=2,
    escapechar=|,
    frame=trBL,
    basicstyle=\footnotesize\ttfamily,
	keywordstyle=\bfseries\ttfamily,
	captiondirection=RTL ,% this requires v14.1 of xepersian and v15.1 of bidi package
	xleftmargin=25pt,
	xrightmargin=10pt,
	framexleftmargin=17pt,
	framexrightmargin=5pt,
	framexbottommargin=4pt
}
\setlatinmonofont[ExternalLocation,BoldFont={cmuntb},ItalicFont={cmunti},BoldItalicFont={cmuntx}]{cmuntt} 
\setlatinmonofont[ExternalLocation,BoldFont={pcrb8a},ItalicFont={pcri8a},BoldItalicFont={pcrbi8a}]{pcrr8a}

\lstset{style=mystyle}

\setdigitfont[Scale=1.1]{ParsiDigits}
\settextfont[Scale=1.2]{XB Yas}
\defpersianfont\nastaliq[Scale=1.6]{IranNastaliq}
\defpersianfont\persiantitle[Scale=1.6]{XB Yas}
\defpersianfont\authername[Scale=1.3]{XB Yas}
\setlatintextfont{Times New Roman}

\begin{document}
\begin{LTR}
\lstset{language=Python, inputencoding={utf8}}
\begin{lstlisting}[texcl,caption={کد پایتون}]
def whoWins(user, comp):
    '''Determine winner,
       Input:   user's and computer's choice
       Output:  "user", "computer" or "tie"
    '''
    winList = [('paper', 'rock'),
               ('scissors', 'paper'),
               ('rock', 'scissors')]
    if user == comp:
        win = 'tie'
    elif (user, comp) in winList:
        win = 'user'
    else:
        win = 'computer'
    return win

def getComp():
    '''Get computer choice
       Input:   None
       Output:  "paper", "rock" or "scissors"
    '''
    import random
    things = ['paper', 'rock', 'scissors']
    pick = random.choice(things)
    print("Computer picks", pick)
    return pick

def getUser():
    '''Get user input
       Input:   None
       Output:  "paper", "rock", "scissors" or "quit"
    '''
    cmdList = ["paper", "rock", "scissors", "quit"]
    while True:
        n = input("Human: enter rock, scissors, paper or quit:")
        if n not in cmdList :
            print ( " Bad input ; try again " )
        else :
            break
    return n

def showResult(score):
    '''Show game result
       Input:   Scores
       Output:  None
    '''
    print('')
    print(format("Game Finished","*^50"))
    print(format("Player"," <20"),format("Score"," <20"))
    print(format('-','-^40'))
    for key, value in score.items():
        print(format(key,' <20'),format(value,' <20'))

if __name__ == "__main__":
    score = {"user":0, "computer":0, "tie":0}
    while True:
        userchoice = getUser()
        if userchoice == "quit":
            break
        compchoice = getComp()
        winner = whoWins(userchoice, compchoice)
        score[winner] += 1
    showResult(score)
\end{lstlisting}
\end{LTR}
\end{document} 