Quantcast

Got Homework?

Connect with other students for help. It's a free community.

  • across
    MIT Grad Student
    Online now
  • laura*
    Helped 1,000 students
    Online now
  • Hero
    College Math Guru
    Online now

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

paulhmorin

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.

  • one year ago
  • one year ago

  • This Question is Closed
  1. Screech
    Best Response
    You've already chosen the best response.
    Medals 0

    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?

    • one year ago
  2. paulhmorin
    Best Response
    You've already chosen the best response.
    Medals 0

    I am attaching a sample. It is a codeacademy problem.

    • one year ago
    1 Attachment
  3. paulhmorin
    Best Response
    You've already chosen the best response.
    Medals 0

    wa=ga(Tyler[subject]) how to dynamically change "Tyler" to any other name without hard coding it.

    • one year ago
  4. Screech
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

    • one year ago
  5. paulhmorin
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

    • one year ago
  6. paulhmorin
    Best Response
    You've already chosen the best response.
    Medals 0

    data not date

    • one year ago
  7. paulhmorin
    Best Response
    You've already chosen the best response.
    Medals 0

    Incidentally, I am not getting an error I just want to know how to do it. I have tried all that I know.

    • one year ago
  8. Screech
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

    • one year ago
  9. paulhmorin
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

    • one year ago
  10. paulhmorin
    Best Response
    You've already chosen the best response.
    Medals 0

    Figured it out. Thanks.

    • one year ago
  11. Screech
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

    • one year ago
    • Attachments:

See more questions >>>

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.