anonymous
  • anonymous
I have 3 dictionaries in a program. All 3 have the same structure. The only difference is that the 3 have different dict names. Depending on input at run time, I need to access the specified dict. How do you code to dynamically change the name of the needed dict in the code. The code is generalized to work with all 3 . I tried making the name a variable but it isn't allowed.
Python Syntax
  • Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
SOLVED
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
jamiebookeater
  • jamiebookeater
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
anonymous
  • anonymous
I'm not grasping the problem. Are you saying for instance that you have three dictionaries: say, dict1, dict2 and dict3 and Python is not allowing you to assign the dictionary designated to be used to a variable dict_to_use = dict1 # or dict2 or dict3 depending on input and then go on to use dict_to_use in the program without regard to which particular dictionary was assigned. I should think it would work. Can you post the sample code which is causing problems?
anonymous
  • anonymous
I am attaching a sample. It is a codeacademy problem.
1 Attachment
anonymous
  • anonymous
wa=ga(Tyler[subject]) how to dynamically change "Tyler" to any other name without hard coding it.

Looking for something else?

Not the answer you are looking for? Search for more explanations.

More answers

anonymous
  • anonymous
Is this from the Just Average / Part of the Whole Codecamy problem. I had to go back and look at my code. What you included looks like the provided base code and doesn't cause a problem and doesn't really do anything. I checked with Idle. I don't know what you are trying to do with the "wa=ga(Tyler[subject])". I even tried that line in the supplied code to see if there was some arcane function. It got an error, of course, looking for ga(). Give me something more to go on to understand what error you are getting. Also, often the Codecamy Q&A Forum includes help where people have run into the same problem you might be having. Often worth a scan when you are stuck.
anonymous
  • anonymous
Yes it is the codeacademy problem. I included their code to show the 3 dict files and the single line just to show that I need to be able to "replace" TYLER with the name of another dict. The code wasn't meant to be functional. I can code the rest. Just hung up on listing all the date from three student dict files without having to duplicate code.
anonymous
  • anonymous
data not date
anonymous
  • anonymous
Incidentally, I am not getting an error I just want to know how to do it. I have tried all that I know.
anonymous
  • anonymous
OK, this is the assignment, right. "Write a function called getClassAverage that takes your student list as input to compute the average of your entire class. In order to do this you will need to use getAverage on each individual student and then take the average of those averages." Look at it in the abstract. "students" is a list whose items are dictionaries. You do not need to translate the string for the student name into the dictionary corresponding to the student. You'll get the student name, if you need it, because its in his/her dictionary. But you shouldn't need it for the problem. Instead, pass "students" to the function "getClassAverage" and within that function go thru the list of dictionaries and process each item/student using getAverage. Because of the way the "average" function works, you will probably want to collect the results from every call to "getAverage" in temporary list that you can pass to "average". Hope I explained it OK and didn't confuse you even more.
anonymous
  • anonymous
Yes, that is what I want to do. I know that I have to pass the student list to getclassaverage. I just don't see what the syntax has to be to take the student list, access the appropriate dictionary, and then extract the lists within. I am an old Cobol & assembler coder. Procedural coding was so more obvious.
anonymous
  • anonymous
Figured it out. Thanks.
anonymous
  • anonymous
I also cut my teeth on Cobol and assembler. My first paying job was in the support area of a mainframe shop. I did a few small Cobol applications but the bulk of my work was in assembler. Later, I migrated to UNIX systems with C and later C++. I was actually quite fond of Perl as it merged a lot of UNIX tools and syntax into a single language. Hadn't programmed in Python before so much of my thinking is Perl oriented and translating operations from Perl into Python. Glad that you got past the hang-up on this exercise.

Looking for something else?

Not the answer you are looking for? Search for more explanations.