Ver Mensaje Individual
  #10 (permalink)  
Antiguo 09/05/2012, 16:25
Avatar de razpeitia
razpeitia
Moderador
 
Fecha de Ingreso: marzo-2005
Ubicación: Monterrey, México
Mensajes: 7.321
Antigüedad: 19 años, 1 mes
Puntos: 1360
Respuesta: Randomizar preguntas y opciones de respuesta

Esto se puede modelar muy bien con clases.

De hecho solo por diversión implemente las clases Opcion, Pregunta, Opciones y Preguntas.

Si hay que escribir mucho código, pero creo que mucho te lo puede autogenerar el editor.

Código Python:
Ver original
  1. from random import shuffle
  2.  
  3. class Opcion:
  4.     def __init__(self, opcion, respuesta, es_valido=False):
  5.         self.opcion = opcion
  6.         self.respuesta = respuesta
  7.         self.es_valido = es_valido
  8.  
  9.     def __eq__(self, other):
  10.         if self is other:
  11.             return True
  12.         if other is None:
  13.             return False
  14.         if not isinstance(other, Opcion):
  15.             return False
  16.         return other.opcion == self.opcion
  17.  
  18.     def __cmp__(self, other):
  19.         if self.__eq__(other):
  20.             return 0
  21.         if not isinstance(other, Opcion):
  22.             return 1
  23.         return cmp(other.opcion, self.opcion)
  24.  
  25.     def __hash__(self):
  26.         return hash(self.opcion)
  27.  
  28.     def __str__(self):
  29.         return "%s) %s" % (self.opcion, self.respuesta)
  30.  
  31. class Opciones:
  32.     def __init__(self):
  33.         self.opciones = set()
  34.  
  35.     def agregar(self, opcion):
  36.         self.opciones.add(opcion)
  37.  
  38.     def borrar(self, opcion):
  39.         self.opciones.pop(opcion)
  40.  
  41.     def limpiar(self):
  42.         self.opciones = set()
  43.  
  44.     def __iter__(self):
  45.         for opcion in self.opciones:
  46.             yield opcion
  47.  
  48.     def __str__(self):
  49.         return '\n'.join(str(opcion) for opcion in sorted(self.opciones, reverse=True))
  50.            
  51.  
  52. class Pregunta:
  53.     def __init__(self, pregunta, opciones):
  54.         self.pregunta = pregunta
  55.         self.opciones = opciones
  56.  
  57.     def es_correcto(self, opcion):
  58.         for opcion_ in self.opciones:
  59.             if opcion == opcion_.opcion and opcion_.es_valido:
  60.                 return True
  61.         return False
  62.  
  63.     def __str__(self):
  64.         return "%s\n%s" % (self.pregunta, self.opciones)
  65.  
  66. class Preguntas:
  67.     def __init__(self):
  68.         self.preguntas = set()
  69.  
  70.     def agregar(self, pregunta):
  71.         self.preguntas.add(pregunta)
  72.  
  73.     def borrar(self, pregunta):
  74.         self.preguntas.pop(pregunta)
  75.  
  76.     def limpiar(self):
  77.         self.preguntas = set()
  78.  
  79.     def __iter__(self):
  80.         aleatorio = sorted(self.preguntas)
  81.         shuffle(aleatorio)
  82.         for pregunta in aleatorio:
  83.             yield pregunta
  84.        
  85.     def __str__(self):
  86.         return '\n'.join(pregunta for pregunta in sorted(self.preguntas))
  87.  
  88. preguntas = Preguntas()
  89.  
  90. opciones = Opciones()
  91. opciones.agregar(Opcion("a", "Azul", es_valido=True))
  92. opciones.agregar(Opcion("b", "Verde"))
  93. opciones.agregar(Opcion("c", "Rojo"))
  94. pregunta = Pregunta("De que color es el cielo?", opciones)
  95. preguntas.agregar(pregunta)
  96.  
  97. opciones = Opciones()
  98. opciones.agregar(Opcion("a", "6"))
  99. opciones.agregar(Opcion("b", "5", es_valido=True))
  100. opciones.agregar(Opcion("c", "4"))
  101. pregunta = Pregunta("Cuantos dedos regularmente tiene una mano?", opciones)
  102. preguntas.agregar(pregunta)
  103.  
  104. opciones = Opciones()
  105. opciones.agregar(Opcion("a", "No"))
  106. opciones.agregar(Opcion("b", "Si", es_valido=True))
  107. opciones.agregar(Opcion("c", "Solo en el dia"))
  108. pregunta = Pregunta("Es el sol una estrella?", opciones)
  109. preguntas.agregar(pregunta)
  110.  
  111. for pregunta in preguntas:
  112.     print pregunta
  113.     respuesta = raw_input()
  114.     if pregunta.es_correcto(respuesta):
  115.         print "Correcto"
  116.     else:
  117.         print "Incorrecto"