Lecture Materials

Questions & Answers

Q: sorry I missed the first part of lecture! can we use things we haven’t learned in class?

A1:  Yes, you can use additional functionality in python, but you will only be evaluated based on the code you write (e.g., if you are using other libraries, we'll only evalulate what you added, not what the libraries provide).

Q: Will Karel be on the second diagnostic?

A1:  No. It will focus on python.

Q: will images and animations be on the diagnostic?

A1:  They are fair game for the diagnostic.

Q: Will images and animation be on the diagnostic?

A1:  They are fair game for the diagnostic.

Q: Where can we find practice problems outside of the practice exam?

A1:  Check out the additional practice handout we just posted on the website! We will also post a preactice diagnostic soon.

Q: will images, graphics, and animation be on the diagnostic?

A1:  They are fair game for the diagnostic.

Q: for the diagnostic, is it all the material we learned in CS106A up to todays class?

A1:  Yes. It will cover material up to today's class.

Q: Chris mentioned that there was something splitting the US (that wasn't the Mississippi river) that caused the density of cities to decrease. Did he ever say what it was?

A1:  He talked about it right before we started class today. Look up the "100th parallel" to get more information.

Q: Can we work on this project in teams (even if it means we aren’t eligible for the prize)? We just want to make something awesome!

A1:  The projects are all individual. But if you want to do something as a group just for fun, that's fine. And we'd be happy to take a look at it, but it wouldn't be eligible for the contest.

Q: Sorry if I missed it, but was there a prize for the contest?

A1:  Yes. We'll also post a handout about the contest that talks about the prizes.

Q: In what years are the dollars measured ? I.e. 2013 dollars, 2015 dollars?

A1:  Not sure what was used as a baseline year.

Q: what was source of the data from all these countries?

A1:  You can find this data and others like it at: https://www.gapminder.org/data/

A2:  Here is the link to the video: https://www.youtube.com/watch?v=jbkSRLYSojo&ab_channel=BBC Not sure where the data comes from. Follow up on Ed and I can ask Chris!

Q: Can you access elements of a string like you access elements of a list (with a for loop using index) or would you have to do it with characters?

A1:  You can use a for loop using index on strings also!

Q: For a dicitionary are the values associated with a key always stored as a list?

A1:  Nope. The value can be any type. In the ages dictionary, the values are ints.

Q: Will what we learned in today's class be on the diagnostic?

A1:  Nested data structures are fair game for the diagnostic.

Q: Does JSON have anything to do with javascript?

A1:  JSON is a format for data that is often used with javascript as well.

Q: is there any limit on the file size to use json.load() on it?

A1:  I think it is the amount of memory available on your computer.

A2:  Depends on how much memory your computer has (and how much of it is allocated to python, when you run it).

Q: Can you store more than one variable in a json?

A1:  For most practical purposes, you'd just have one variable, but you could have a list and then break out the individual elements in your program.

Q: Could you please define a nested datastructure?

A1:  When you have a structure (like a list or dictionary) inside another structure. So a dictionary where the values were lists would be an example of a nested data structure.

Q: can we have a jason file with two seperate dictionaries? Or does every dictionary need its own file?

A1:  For most practical purposes, you'd just have one variable, but you could have a list and then break out the individual elements in your program.

Q: would it make sense to create a seperate function that opens the json file or should it just be done in main?

A1:  opening the json file is just one line of code so you can keep it in main.

Q: What was the command to save a json file?

A1:  json.dump(data, open('ages.json', 'w'))

Q: so since json opens an entire file at once, are json files usually intended to data type lists when read in?

A1:  They are generally lists or dictionaries.

Q: Why would you need Json when you could just make the dictionary in your prorgram?

A1:  This way you don’t have to recreate the dictionary in every program. You can load it from a file and use it more easily in lots of files.

Q: If we want to use helper functions using text files, can the contents of the file be passed as parameters or do you need to open the file in each helper function?

A1:  You can read the data into one structure and then just pass that structure (or pieces of it).

Q: Who creates the JSON file? How do we decide what’s the best nested data structure if we’re creating a JSON file ourselves?

A1:  You can create the JSON file. Deciding the best nested data strucure is another piece of the art of programming. You will get practice with this in section this week.

A2:  Data stucture design is an art in itself (like writing code). There are multiple ways to represent some data set. You want to choose a representation that is easy for your program to work with and efficient to represent.

Q: How do you create a Json file?

A1:  json.dump(data, open('ages.json', 'w')) is how you put data into a json file

Q: Where is the best place to find a summary of animation commands? I’m having some trouble finding them in the handouts section and the python reader.

A1:  Check out slides in Lecture 13 and the code from that lecture. That should have most of the relevant syntax.

Q: How do you create a text file in pycharm?

A1:  file->new file and name it name_of_your_choice.txt

Q: does json.load(open(‘weather.json’)) produce a list to be stored in a variable always?

A1:  It depends on what data is stored in the file. If the data is a list, then when you read it, you will get a list.

Q: I’m trying to code and follow along the lecture in pycharm, but every time I run the program it says “/Library/Frameworks/Python.framework/Versions/3.8/bin/python3: can't open file 'weather.py': [Errno 2] No such file or directory” Am I doing something wrong?

A1:  What directory are you in on your computer? Can you type ls into terminal? (or dir if you are on windows)

Q: is there a way to round the float to 2 decimal places etc?

A1:  The round function allows for specifying decimal places. For example: x = round(5.76543, 2) will round the number to 2 decimal points and return it (to be stored in x)

A2:  Yes! the round function takes an optional parameter which will let you do this.

Q: Should we use nestled data stuctures in our current assignment?

A1:  It's up to you to determine what sort of data structure you need for the assignment 5. But you shouldn't worry if you can do it all without nested data structures.

Q: for assignment 5, what lectures do we need?

A1:  Up to (and including) Oct 23.

Q: Could use dictionaries to hold animations?

A1:  yea :)

Q: What's the difference between json and opening a file?

A1:  json is a type of file. You can open a json file into a dictionary using json.load which is pretty neat because you cannot as easily open other kinds of files (like a txt file discussed in the files lecture).

Q: Are we allowed to import external libraries/modules for extensions in the assignments? (the extension would still be related to the assignment itself)

A1:  Yes you can do that for extensions. Make sure to turn in two versions (basic and extended) if you do that.

A2:  Yes, you can use additional functionality in python, but you will only be evaluated based on the code you write (e.g., if you are using other libraries, we'll only evalulate what you added, not what the libraries provide).

Q: so are the moves also keys?

A1:  Yes, the moves are key in the "inner" dictionary.

Q: so is this a dictionary of dictionaries?

A1:  Yes!

Q: How do you keep track mentally of these multiple layers of dictionaries?

A1:  Drawing pictures of your data structures helps.

Q: So to loop through each of the locations, can we use a for each loop to get each of the keys?

A1:  Yes.

Q: what does the /n do after the Stanford Adventure Game?

A1:  It prints an extra "newline" (return) on the screen.

Q: does the variable “data” store a dictionary here?

A1:  Yes!

Q: What is current_room_name = START for?

A1:  That's the room the player starts in. START is just a constant.

Q: How does START store the key for tresidder?

A1:  It's a constant.

Q: Re earlier qs about directory (sorry I couldn’t reply to Juliette’s answer for some reason), I am in the Lecture 18 folder with all the files. But none of the programs will run on Pycharm when I type python3 name of file.

A1:  Strange. Can you post on Ed about ths? It will be easier for me to follow up.

A2:  Wait try typing: cd Lecture18 into the terminal. Then try to run the code.

Q: do we need to check if next_room exists in current_room_data[‘moves’] before we try to access it? or does the lookup already take care of it?

A1:  We're assuming next_room will be a valid key in the dictionary.

Q: so json files are just variables?

A1:  They are text representations of data structures that can be read into a variable.

Q: What does current_room_name = START mean?

A1:  It sets the room that the player starts in. START is a constant.

A2:  START is a constant sorting the first room. So at the beginning we se thte current_room_name equal to this constant.

Q: what does the line next_room_command = input(‘? ‘) do?

A1:  it reads a command from the user and stores it in the variable next_room_command

A2:  Asks the user to input the next room commmand

Q: So then why would we naturally start at Tresidder?

A1:  Doesn't everyone live at Tresidder? :-). It's just a choice that was made for this demo program.

Q: can we not say something like “if value not in dictionary?”

A1:  You could say "if value not in dictionary.values()" to check if a value is in the set of values in the dictionary.

Q: why are comma separated values so beneficial?

A1:  It's just a simple way to represent a list of information.

Q: whats a csv again?

A1:  comma separated values It's a format for representing a list of data simply.

Q: Like “comma” separated files, could we create any “special character” separated file?

A1:  You could, but comma separated files is a pretty standard format. For example, many spreadsheets will allow you to save the spreadsheet as a csv file, or read a csv file into a spreadsheet.

Q: what does the circle diameter represent?

A1:  It's proportional to the population of the country.

Q: Is there a way to access a specific line when calling a text file?

A1:  You'd need to read the file up to the line you want.

Q: is there a way to split a line at multiple places

A1:  Not with one call to the split function.

Q: Why did you split the data by period and not with a comma? Aren’t csv files split by commas?

A1:  That was splitting the file name, not the contents of the file.

Q: I don’t really understand the split function - where can I review this?

A1:  https://cs.stanford.edu/people/nick/py/python-string.html talks about string functions, including split

Q: could you have opened and done the for loop in one line?

A1:  Not sure what you mean by do the loop in one line. The body of the loop loop could be done in one line, but it would be less clear.

Q: why does file_name.split use “.”? where is . in the filename?

A1:  live answered

Q: sorry, what does file_name.split(‘.’)[0] do? where is the period in the file?

A1:  live answered

Q: Why does Chris have key = file_name.split(‘ . ‘)[0] written at the top of add_data() ?

A1:  live answered

Q: if we have a string that’s wrapped in parentheses like so, ‘(hello), (goodbye)’, is there a way to create a list that is [‘hello’, ‘goodbye’]?

A1:  You can parse the string to find all ( and replace it with ‘ and find alll ) and replace it with ‘

Q: what is the key = file_name.split(‘ . ‘)[0] doing in the code?

A1:  live answered

Q: why not just pop the first value?

A1:  You could. It's a stylistic choice. Here, it's clearer to indicate which parts of the list are being assigned to which variable.

Q: How did Chris convert the excel file into a csv?

A1:  Excel lets you save in csv format with "Save As"

Q: oh i meant could you do for line in open(file name) or do you have to do file = open(filename) as a separate line than the for loop

A1:  It needs to be a separate line than the for loop.

Q: what is the pycharm shortcut chris is using to uncomment lines of code

A1:  highlight the code and hit ctrl and / at the same time

Q: will data visualization and the content of the assignment that will be released on Friday be on the diagnostic?

A1:  Nested data structures are fair game for the diagnostic, but we wouldn't ask you something as complex as this program or assignment 6.

Q: where did the data dictionary come from?

A1:  You can find data for this (and other) application at: https://www.gapminder.org/data/

Q: is there any real difference between using elif and if?

A1:  Yes. elif is the "else" portion of an existing if statement. if (by itself) is a standalone statement.

Q: How does key know which value is life pop and gdp?

A1:  "pop" and "gdp" are keys.

Q: Why do you not have to ‘build up a dictionary’ for the [key] like what we did for the other keys?

A1:  that was the last layer. The dictionaries already existed, we just had to put in a key/value pair

Q: could we trace the variable key

A1:  Not sure what you mean by trace the variable. But you could keep track of the value of the variable.

Q: is there a keyboard shortcut for commenting/uncommenting?

A1:  CTRL+/ to comment CTRL+shift+/ to uncomment

A2:  highlight the code you want to comment/uncomment hit ctrl and / at the same time.

Q: will today’s content be included in the diagnostic or is it only up to last friday’s lecture?

A1:  Nested data structures are fair game for the diagnostic.

Q: what does the circle size mean again?

A1:  The population of the country.

Q: where can I find what chris just coded?

A1:  http://web.stanford.edu/class/cs106a/lectures/18-NestedStructures2/

Q: since the visualization goes by country, do we need to create another json per country like in option A?

A1:  No we made just one big variable that had all the data!!

Q: We have been doing graphics for the nested data programs in but more focus is done on the data and not the graphics part. Does that mean when we are studying for diagnostic we focus on nested data concepts and not the graphics related to those nested data programs?

A1:  yes :)

Q: what can we do to prepare for the diagnostic? :(

A1:  There will be a practice diagnostic. Also there were be extra problems provided in section that you can practice with.

A2:  There is a handout called additional practice on the website. We will also post a practice diagnostic soon.

Q: Should we practice building up our own nested dictionaries for the diagnostic?

A1:  yes :)

Q: For Assignment 5 credit card total, the suggestion says using dictionary would be helpfu. Does that mean, to solve the problem, we need to first build a dictionary?

A1:  yes :)

Q: How did you make the lists for life, pop, and gdp?

A1:  i didn’t make lists, i went with the “option B” strategy where i mapped year -> country -> key/value pairs

Q: What is the function that gets moves the dictionaries into the json file

A1:  json.dump! Its in the slides for reference