I am having some difficulties understanding how to implement the dict() in Pyth v3.2 at least in the way that I would like to use it. Basically I first create a list. I then randomly reorganize the list. Then I want to create a dictionary using that list, assigning the indexed item from the list to a key in the dictionary. After running the program, the user is prompted to input a variable which is supposed to be equal to one of the keys of the dictionary. But I do not want the users input to be compared and weighed against the key itself, but rather the object assigned to the key (obviously...why the hell else bother creating the dictionary? :P). When I do this though, python starts complaining that variables haven't
t been declared. I thought this was the most direct way to compare the two but now I am completely stuck. No clue how to solve this. -
-
list_A = ['Obj1','Obj2','Obj3','Obj4','Obj5']
-
-
shuffle(list_A)
-
-
dict_A = {1:list_A[0], 2:list_A[1],3:list_A[2],4:list_A[3]
-
,5:list_A[4],6:list_A[5]}
-
-
z = input('enter something')
-
-
if dict_A[1] == list_A[0]:
-
#does dict_A item '1' (i.e. list_A),
-
#equal the actual list_A index of 0?
-
print('blah,blah,blah)
-
else:
-
something, something nice, something bad
-
-
2 3560
You would search the entire dictionary and compare the dictionary's value to the look up variable. This does not take that long unless the dictionary is huge. - dict_A = {1:"abc", 2:"def", 3:"ghi"}
-
look_up = "def"
-
for key in dict_A:
-
if dict_A[key] == look_up:
-
print "Found", key, dict_A[key]
Your code has some problems:
-shuffle() needs to be imported first. 'import random' should work.
-This page http://www.tutorialspoint.com/python/number_shuffle.htm says shuffle() should be called using a static 'random' object. 'random.shuffle(myList)'
-In line 7, you are asking for a 6th list item, but only 5 exist. I think thats not allowed.
-line 14 is missing a ' to end the string
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Kyle E |
last post by:
Ok, I have a little tiny problem, a beginner problem, that I am overlooking.
I am writing a Information Gathering Program, that takes user input from
scripted questions and prints them in a handy...
|
by: N?ant Humain |
last post by:
I have just begun learning Python so that I can write a simple script
to make modification of a file used by another Python script easier.
This file is basically a list of regular expressions. What...
|
by: Steve Perry |
last post by:
I have a serialized list and dictionary stored in a file, with the
following format:
mydata:
color
origin
datasource1 {server:"server1.domain" username:"client"
password:"secret"}
Is...
|
by: chuck |
last post by:
I need some help with validating user input. I am writing a C computer
program for an intro to C course. Here is the situation. I am
creating an application that will do currency conversions. ...
|
by: JB |
last post by:
Hi All,
I was wondering if there was an easy and straightforward way to bind
elements in a ListView control, with the corresponding elements in a
List or Dictionary.
Ideally I'd like a...
|
by: vinkumar |
last post by:
Hi,
I have to search for string and replace with user input value using js. I have used inner HTML (objExplorer.Document.body.innerHTML) to get user input.
(Eg: If user inputs web port no. as...
|
by: pchadha20 |
last post by:
Hello Guys,
I am Not able to user synonyms based on dictionary tables like
..when i write the command like
SELECT * FROM USER_SOURCE
then error comes like
ERROR at line 1:
ORA-00980:...
|
by: Tamer Ibrahim |
last post by:
Hi,
I'm trying to validate user input date using range validator on a text box .
This code is throwing this exception
"The value 'DateTime.Now;' of the MaximumValue property of 'rvQDate'...
|
by: =?Utf-8?B?THVpZ2k=?= |
last post by:
Hi all,
having a List<stringand Dictionary<int, stringhow can I check that every
string in the list is also in the Dictionary (and viceversa)?
(and raise an exception when not).
Thanks in...
|
by: golnaz hgh |
last post by:
the xml file is like this:
<publist>
<book>
A book entry has the following fields:
o Title;
o One or more authors;
o Publisher;
o Year
</book>
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| | |