<s> '''<STR_LIT>''' <EOL> import random <EOL> import sqlite3 <EOL> import sys <EOL> from PyQt4 import QtGui <EOL> max_nb = <NUM_LIT:100> <EOL> prompt = "<STR_LIT>" . format ( max_nb ) <EOL> db_file_name = "<STR_LIT>" <EOL> class GameWindow ( QtGui . QMainWindow ) : <EOL> def __init__ ( self ) : <EOL> super ( ) . __init__ ( ) <EOL> self . create_layout ( ) <EOL> def create_layout ( self ) : <EOL> self . setWindowTitle ( "<STR_LIT>" ) <EOL> self . resize ( <NUM_LIT> , <NUM_LIT:200> ) <EOL> exit_action = QtGui . QAction ( '<STR_LIT>' , self ) <EOL> exit_action . setShortcut ( '<STR_LIT>' ) <EOL> exit_action . setStatusTip ( '<STR_LIT>' ) <EOL> exit_action . triggered . connect ( self . safe_exit ) <EOL> menubar = self . menuBar ( ) <EOL> actions_menu = menubar . addMenu ( '<STR_LIT>' ) <EOL> actions_menu . addAction ( exit_action ) <EOL> self . statusBar ( ) . showMessage ( '<STR_LIT>' ) <EOL> self . label = QtGui . QLabel ( "<STR_LIT>" , self ) <EOL> self . label . move ( <NUM_LIT:10> , <NUM_LIT> ) <EOL> self . entry = QtGui . QLineEdit ( self ) <EOL> self . entry . returnPressed . connect ( self . analyze_guess ) <EOL> self . entry . move ( <NUM_LIT:200> , <NUM_LIT> ) <EOL> self . new_game_button = QtGui . QPushButton ( "<STR_LIT>" , self ) <EOL> self . new_game_button . clicked . connect ( self . new_game ) <EOL> self . waiting_for_game ( ) <EOL> self . show ( ) <EOL> def safe_exit ( self ) : <EOL> '''<STR_LIT>''' <EOL> print ( "<STR_LIT>" ) <EOL> QtGui . qApp . quit ( ) <EOL> def waiting_for_game ( self ) : <EOL> self . label . hide ( ) <EOL> self . entry . hide ( ) <EOL> self . new_game_button . show ( ) <EOL> def new_game ( self ) : <EOL> self . label . show ( ) <EOL> self . entry . show ( ) <EOL> self . new_game_button . hide ( ) <EOL> self . goal = random . randint ( <NUM_LIT:1> , max_nb ) <EOL> def analyze_guess ( self ) : <EOL> guess = self . safe_conversion ( self . entry . text ( ) ) <EOL> if guess : <EOL> if guess == self . goal : <EOL> message = "<STR_LIT>" <EOL> self . waiting_for_game ( ) <EOL> elif guess < self . goal : <EOL> message = "<STR_LIT>" . format ( guess ) <EOL> else : <EOL> message = "<STR_LIT>" . format ( guess ) <EOL> self . statusBar ( ) . showMessage ( message ) <EOL> self . entry . setText ( '<STR_LIT>' ) <EOL> def safe_conversion ( self , guess ) : <EOL> try : <EOL> guess = int ( self . entry . text ( ) ) <EOL> return guess <EOL> except ValueError : <EOL> self . statusBar ( ) . showMessage ( "<STR_LIT>" ) <EOL> return False <EOL> def create_db ( ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> db = sqlite3 . connect ( db_file_name ) <EOL> cursor = db . cursor ( ) <EOL> cursor . execute ( <EOL> '''<STR_LIT>''' ) <EOL> db . commit ( ) <EOL> except Exception as e : <EOL> db . rollback ( ) <EOL> raise e <EOL> finally : <EOL> db . close ( ) <EOL> def retrieve_data ( ) : <EOL> '''<STR_LIT>''' <EOL> create_db ( ) <EOL> db = sqlite3 . connect ( db_file_name ) <EOL> cursor = db . cursor ( ) <EOL> cursor . execute ( '''<STR_LIT>''' ) <EOL> stats = cursor . fetchone ( ) <EOL> db . close ( ) <EOL> if stats is None : <EOL> print ( "<STR_LIT>" ) <EOL> return <NUM_LIT:0> , <NUM_LIT:0> , False <EOL> else : <EOL> print ( """<STR_LIT>""" . format ( <EOL> stats [ <NUM_LIT:1> ] , stats [ <NUM_LIT:2> ] / stats [ <NUM_LIT:1> ] ) ) <EOL> return stats [ <NUM_LIT:1> ] , stats [ <NUM_LIT:2> ] , True <EOL> def save_data ( nb_games , nb_guesses , data_exists ) : <EOL> '''<STR_LIT>''' <EOL> db = sqlite3 . connect ( db_file_name ) <EOL> cursor = db . cursor ( ) <EOL> if data_exists : <EOL> cursor . execute ( '''<STR_LIT>''' , ( nb_games , "<STR_LIT>" ) ) <EOL> cursor . execute ( '''<STR_LIT>''' , ( nb_guesses , "<STR_LIT>" ) ) <EOL> else : <EOL> cursor . execute ( '''<STR_LIT>''' , <EOL> { '<STR_LIT:name>' : "<STR_LIT>" , '<STR_LIT>' : nb_games , '<STR_LIT>' : nb_guesses } ) <EOL> db . commit ( ) <EOL> db . close ( ) <EOL> def game ( ) : <EOL> '''<STR_LIT>''' <EOL> nb_attempts = <NUM_LIT:0> <EOL> number = random . randint ( <NUM_LIT:1> , max_nb ) <EOL> while True : <EOL> guess = safe_input ( ) <EOL> nb_attempts += <NUM_LIT:1> <EOL> if guess == number : <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" . format ( nb_attempts ) ) <EOL> return nb_attempts <EOL> elif guess == - <NUM_LIT:1> : <EOL> print ( "<STR_LIT>" . format ( number ) ) <EOL> return False <EOL> elif guess < number : <EOL> print ( "<STR_LIT>" ) <EOL> elif guess > number : <EOL> print ( "<STR_LIT>" ) <EOL> def goodbye ( nb_games , total_attempts , data_exists ) : <EOL> '''<STR_LIT>''' <EOL> if nb_games != <NUM_LIT:0> : <EOL> print ( "<STR_LIT>" . format ( <EOL> nb_games , total_attempts ) ) <EOL> print ( "<STR_LIT>" . format ( <EOL> total_attempts / nb_games ) ) <EOL> save_data ( nb_games , total_attempts , data_exists ) <EOL> print ( "<STR_LIT>" ) <EOL> def play_games ( ) : <EOL> '''<STR_LIT>''' <EOL> nb_games , total_attempts , data_exists = retrieve_data ( ) <EOL> if nb_games == <NUM_LIT:0> : <EOL> print ( "<STR_LIT>" ) <EOL> else : <EOL> print ( "<STR_LIT>" ) <EOL> while True : <EOL> result = game ( ) <EOL> if result : <EOL> total_attempts += result <EOL> nb_games += <NUM_LIT:1> <EOL> ans = input ( "<STR_LIT>" ) <EOL> if ans != '<STR_LIT:y>' : <EOL> break <EOL> else : <EOL> break <EOL> goodbye ( nb_games , total_attempts , data_exists ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> app = QtGui . QApplication ( sys . argv ) <EOL> window = GameWindow ( ) <EOL> app . aboutToQuit . connect ( window . safe_exit ) <EOL> sys . exit ( app . exec_ ( ) ) </s>
