[pyar] Randomizar diccionarios para un programa.

Angel Freire cuerty en gmail.com
Jue Mayo 10 01:41:39 ART 2012


Python 2.7.2+ (default, Oct  4 2011, 20:06:09)
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import random
>>> preguntas_respuestas = {'Cuanto es 1+1':('a: 2','b: 5','c: 8'),'Tu mama
te quiere?':['a: si', 'b: no', 'c: tal vez']}
>>> pregunta, respuestas = random.choice(preguntas_respuestas.items())
>>> pregunta
'Tu mama te quiere?'
>>> respuestas
['a: si', 'b: no', 'c: tal vez']


2012/5/10 Eugenia Bahit <eugeniabahit en gmail.com>

> El 10 de mayo de 2012 01:05, Javier Marín <nano.world.contact en gmail.com>escribió:
>
>> print clave, preguntas_respuestas[clave]
>
>
> Lo mismo que te dijeron antes. Debería ser:
> print clave_azar, preguntas_respuestas[clave_azar]
>
> porque si vos hacés preguntas_respuestas[*clave*] ahí "clave" no es UNA
> clave, es una lista de claves:
>
>
> >>> preguntas_respuestas = {'Cuanto es 1+1':('a: 2','b: 5','c: 8'),'Tu
> mama te quiere?':['a: si', 'b: no', 'c: tal vez']}
> >>>
> * clave = preguntas_respuestas.keys()
> >>> print clave
>
> ['Tu mama te quiere?', 'Cuanto es 1+1']
> *
> *>>> print preguntas_respuestas[clave]*
>
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> TypeError: unhashable type: 'list'
> >>> *print clave_azar, preguntas_respuestas[clave_azar]
>
> Cuanto es 1+1 ('a: 2', 'b: 5', 'c: 8')
> *
>
>
> ¿Se entiende? Clave es una LISTA y una lista no la podés pasar como clave.
> Eso es lo que te dice el error.
>
> Saludos!!
>
> _______________________________________________
> pyar mailing list pyar en python.org.ar
> http://listas.python.org.ar/listinfo/pyar
>
> PyAr - Python Argentina - Sitio web: http://www.python.org.ar/
>
> La lista de PyAr esta Hosteada en USLA - Usuarios de Software Libre de
> Argentina - http://www.usla.org.ar
>



-- 
http://blog.cuerty.com

"If you want to set off and go develop some grand new thing, you don't need
millions of dollars of capitalization. You need enough pizza and Diet Coke
to stick in your refrigerator, a cheap PC to work on and the dedication to
go through with it."
- John Carmack
------------ próxima parte ------------
Se ha borrado un adjunto en formato HTML...
URL: <http://listas.python.org.ar/pipermail/pyar/attachments/20120510/d95e8649/attachment.html>


More information about the pyar mailing list