ex 1 part 1
This commit is contained in:
		| @ -1,3 +1,5 @@ | |||||||
|  | 50/50 for ever door all the time! | ||||||
|  |  | ||||||
| The Monty Hall Problem | The Monty Hall Problem | ||||||
| ====================== | ====================== | ||||||
|  |  | ||||||
|  | |||||||
							
								
								
									
										
											BIN
										
									
								
								exercises/1-monty-hall-problem/__pycache__/models.cpython-34.pyc
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								exercises/1-monty-hall-problem/__pycache__/models.cpython-34.pyc
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										
											BIN
										
									
								
								exercises/1-monty-hall-problem/__pycache__/views.cpython-34.pyc
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								exercises/1-monty-hall-problem/__pycache__/views.cpython-34.pyc
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							| @ -1,6 +1,52 @@ | |||||||
| #from models import | from models import Doors | ||||||
| from views import View | from views import View | ||||||
|  |  | ||||||
| class Controller: | class Controller: | ||||||
| 	def __init__(self): | 	def __init__( self ): | ||||||
| 		pass | 		self.view = View() | ||||||
|  | 		self.name = self.view.get_name() | ||||||
|  | 		self.start() | ||||||
|  |  | ||||||
|  | 	def start( self ): | ||||||
|  |  | ||||||
|  | 		selected = int( self.view.display_doors() ) | ||||||
|  | 		self.doors = Doors( selected, self.name ) | ||||||
|  |  | ||||||
|  | 		self.host_twist() | ||||||
|  |  | ||||||
|  | 		return True | ||||||
|  |  | ||||||
|  | 	def host_twist( self ): | ||||||
|  |  | ||||||
|  | 		message ="" | ||||||
|  |  | ||||||
|  | 		for i in range( 1, 4 ): | ||||||
|  | 	 | ||||||
|  | 			if i == self.doors.selected: | ||||||
|  | 				message += "[ X ] " | ||||||
|  | 			elif i == self.doors.open_door: | ||||||
|  | 				message += "[ Goat ] " | ||||||
|  | 			else: | ||||||
|  | 				message += "[ " + str(i) + " ] " | ||||||
|  | 				other_selection = i | ||||||
|  |  | ||||||
|  | 		selection = self.view.host_twist( message ) | ||||||
|  |  | ||||||
|  | 		if selection: | ||||||
|  | 			self.doors.selected = other_selection | ||||||
|  | 			self.doors.switch = True | ||||||
|  | 		return self.out_come() | ||||||
|  |  | ||||||
|  | 	def out_come( self ): | ||||||
|  | 		if self.doors.out_come(): | ||||||
|  | 			do = self.view.winner() | ||||||
|  | 		else: | ||||||
|  | 			do = self.view.loser() | ||||||
|  |  | ||||||
|  | 		if do == 'n': | ||||||
|  | 			exit() | ||||||
|  |  | ||||||
|  | 		self.start() | ||||||
|  | 		return True | ||||||
|  |  | ||||||
|  | Controller() | ||||||
|  | |||||||
							
								
								
									
										
											BIN
										
									
								
								exercises/1-monty-hall-problem/default.db
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								exercises/1-monty-hall-problem/default.db
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							| @ -0,0 +1,37 @@ | |||||||
|  | import random | ||||||
|  |  | ||||||
|  | class Doors: | ||||||
|  |     def __init__( self, selected, name ): | ||||||
|  |         self.name = name | ||||||
|  |         self.switch = False | ||||||
|  |         self.selected = selected | ||||||
|  |         self.car = random.randint( 1,3 ) | ||||||
|  |         self.open_door = self.host_twist() | ||||||
|  |  | ||||||
|  |     def host_twist( self ): | ||||||
|  |         open_door = random.randint( 1,3 ) | ||||||
|  |         if open_door == self.car or open_door == self.selected: | ||||||
|  |             return self.host_twist() | ||||||
|  |         return open_door | ||||||
|  |  | ||||||
|  |     def out_come( self ): | ||||||
|  |  | ||||||
|  |         if self.selected == self.car: | ||||||
|  |             logDB( self.name, self.switch, True) | ||||||
|  |             return True | ||||||
|  |         else: | ||||||
|  |             logDB( self.name, self.switch, False) | ||||||
|  |             return False | ||||||
|  |  | ||||||
|  | def logDB( name, switch, outcome ): | ||||||
|  |     import sqlite3 | ||||||
|  |  | ||||||
|  |     conn = sqlite3.connect('default.db') | ||||||
|  |     c = conn.cursor() | ||||||
|  |  | ||||||
|  |     c.execute( "INSERT INTO games (name, switch, outcome) values (?,?,?) ", ( name, switch, outcome ) ) | ||||||
|  |  | ||||||
|  |     conn.commit() | ||||||
|  |  | ||||||
|  |     conn.close() | ||||||
|  |     return True | ||||||
							
								
								
									
										6
									
								
								exercises/1-monty-hall-problem/schema.sql
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								exercises/1-monty-hall-problem/schema.sql
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,6 @@ | |||||||
|  | CREATE TABLE games ( | ||||||
|  | id INTEGER DEFAULT NULL PRIMARY KEY AUTOINCREMENT, | ||||||
|  | name TEXT DEFAULT NULL, | ||||||
|  | switch TEXT DEFAULT NULL, | ||||||
|  | outcome TEXT DEFAULT NULL | ||||||
|  | ); | ||||||
| @ -1,3 +1,24 @@ | |||||||
| class View: | class View: | ||||||
| 	def __init__(self): |     def __init__( self ): | ||||||
| 		pass |         print( "Welcome to the Monty Hall Problem!") | ||||||
|  |  | ||||||
|  |     def get_name( self ): | ||||||
|  |         return input( "Please enter your name: " ) | ||||||
|  |  | ||||||
|  |     def display_doors( self ): | ||||||
|  |         print( 'Doors: [ 1 ] [ 2 ] [ 3 ]' ) | ||||||
|  |         return input( 'Select door: ' ) | ||||||
|  |  | ||||||
|  |     def host_twist( self, message ): | ||||||
|  |         print( "The host will revel where one goat is..." ) | ||||||
|  |         print( message ) | ||||||
|  |         print( "Would you like to change your selection? [n/y]" ) | ||||||
|  |         return input( "[no]: ") | ||||||
|  |  | ||||||
|  |     def winner( self ): | ||||||
|  |         print( "You WIN!!!" ) | ||||||
|  |         return input("Play again?[y]") | ||||||
|  |  | ||||||
|  |     def loser( self ): | ||||||
|  |         print( "You Lose!!!" ) | ||||||
|  |         return input("Play again?[y]") | ||||||
		Reference in New Issue
	
	Block a user