1
+ # 36- El sombrero selecionador
2
+ import random
3
+
4
+ questions = {}
5
+
6
+ for i in range (1 , 11 ):
7
+ questions [i ] = ['a' ,'b' ,'c' ,'d' ]
8
+
9
+ class SortingHat ():
10
+
11
+ def __init__ (self , questions : dict ):
12
+ self .questions = questions
13
+
14
+ def ask_questions (self ) -> dict :
15
+ frontend = 0
16
+ backend = 0
17
+ mobile = 0
18
+ data = 0
19
+
20
+ for quest in self .questions :
21
+ print (f"Pregunta: { quest } " )
22
+ print (f"Opciones: " )
23
+ for index , responses in enumerate (self .questions [quest ]):
24
+ print (f"{ index + 1 } . { responses } " )
25
+ while True :
26
+ response = input ("Respuesta elegida: " )
27
+ match response :
28
+ case '1' :
29
+ frontend += 1
30
+ break
31
+ case '2' :
32
+ backend += 1
33
+ break
34
+ case '3' :
35
+ mobile += 1
36
+ break
37
+ case '4' :
38
+ data += 1
39
+ break
40
+ case _:
41
+ print ("Valor incorrecto, vuelve a intentar" )
42
+ return {
43
+ 'frontend' : frontend ,
44
+ 'backend' : backend ,
45
+ 'mobile' :mobile ,
46
+ 'data' : data
47
+ }
48
+
49
+ def election (self , results : dict , name ):
50
+ max_afinity = max ([x for x in results .values ()])
51
+ afinity = {k : v for k , v in results .items () if v == max_afinity }
52
+ if len (afinity ) > 1 :
53
+ final_election = random .choice (list (afinity .keys ()))
54
+ print (
55
+ f"""\n Hmmmm... Ha sido una decisión muy complicada, {
56
+ name } .\n ¡Pero finalmente tu casa será { final_election } !"""
57
+ )
58
+ else :
59
+ final_election = list (afinity .keys ())[0 ]
60
+ print (f"\n Enhorabuena, { name } .\n ¡Tu casa será { final_election } !" )
61
+
62
+
63
+ print ("\n ¡Bienvenido a Hogwarts, la escuela de programación para magos y brujas del código!" )
64
+ print ("El sombrero seleccionador decidirá cuál es tu casa como programador." )
65
+
66
+ name = input ("\n ¿Cuál es tu nombre? " )
67
+
68
+ sortinghat = SortingHat (questions )
69
+
70
+ sortinghat .election (sortinghat .ask_questions (), name )
0 commit comments