Lecture Materials

Questions & Answers

Q: how we doing today friends!

A1:  live answered

Q: Will the diagnostic review sessions be recorded_

A1:  But we’d recommend coming in person if you can!

A2:  Yep

Q: will the review sessions be recorded?

A1:  But we’d recommend coming in person if you can!

A2:  Yep

Q: So when you use x.replace(), it creates a new string (unless you specify x = x.replace())?

A1:  Even then, you’re creating a new string and storing its address in x

Q: Is “line” pre-defined? How does it know it doesn’t mean “word” or some other delimited unit?

A1:  "line" is just the name of a string variable. When you read the file, python reads it line by line.

Q: Is line a defined element (like pixel in image) or is it just a variable name?

A1:  It’s a variable name, but so is pixel

Q: is there a way to go the opposite way? like go up the text rather than down?

A1:  Not with a file

Q: Is line stored as a list of characters?

A1:  It’s a string

A2:  line is a string

Q: something I'm confused abt: You store file as a 'variable' type; does that mean it is immutable and that you're only modifying a copy in Python?

A1:  You can think of it as a reference to a file in your computer somewhere - you can change it to refer to another file, if you’d like, and also ways to write to that file (although we won’t be talking about that much)

Q: the /n is there just automatically because of being a .txt program?

A1:  Yes. The \n is the "return" character at the end of each line in a text file.

Q: how come there is still a space between the stanzas?

A1:  There are blank lines between stanzas in the original text file.

A2:  live answered

Q: how does python know that elem is a single element in an array, or line is a whole line in text?

A1:  The collection you’re iterating over will give you back whatever an individual element is

Q: Should we just take for granted that the “for-each” loop uses the line as the unit of measure? What about each character, etc.?

A1:  It’ll always give you back a line

Q: Won't line = line.strip() replace the original line with the stripped line?

A1:  It won’t replace it in the text, but the line variable will now be stripped, yes

A2:  Yes. The stripped line is just the original line without the newline at the end.

A3:  Won't line = line.strip() replace the original line with the stripped line?

Q: Why didn't the second loop work? Wouldn't it repeat the same output as above?

A1:  We just talked about it, but the file doesn’t reset to the start at the end of the first for loop.

Q: when it says the “file object,” does that refer to f? or what does it refer to?

A1:  It’s f!

Q: still don’t get line.strip role =/

A1:  Every line in a file includes a “\n” character at the end, which represents a line break. Printing already adds a new line, so we don’t need the \n from a line. .strip() removes that newline character from the line.

A2:  Here, it just removed the "return" character at the end of each line in the file.

Q: So next() would have returned the first line or the second?

A1:  The first!

Q: What does next(f) do again?

A1:  Skips the first line of the file

Q: Is there a way to read a file word-by-word and not just line-by-line?

A1:  No, iterating over a file will always give you a line. You could break that line up into words, though!

Q: How do you break a line up into words?

A1:  Here, we're just treating each line in the file as a "word" (or phrase) for the game.

A2:  Each line is just a string, so you could use string operations on it to break it up.

A3:  There’s a function called .split() which returns a list of the space-separated words in the string

Q: could you also say word.append(line)?

A1:  As opposed to?

Q: how can you ensure that you dont reuse words?

A1:  You can’t!

Q: Does “Pat Hanahan” count as one word or two?

A1:  In this case, one ‘word’, which is maybe a misomer :)

Q: should it be for line in f?

A1:  Yep, I’ll correct him in a sec

Q: How can we export a game we create (e.g. heads up) to a different user friendly userface (anything other than pycharm)?

A1:  It’s a little tricky in Python, unfortunately. There are some ways to do it but it’s a little beyond what we know how to do right now.

Q: Q: could you also say word.append(line)? A: As opposed to? Chris wrote word.append(word) within his for line in list function so I was just wondering if we could append lines onto our new list instead of words

A1:  I’m not sure I understand - he’s appending to words, which is the list that is getting returned

Q: why does it say ‘as f’ but then line in ‘file’?

A1:  Typo! It should be for line in f

Q: is that for line in f instead of line in file (as Brahm mentioned previously)

A1:  Yep

Q: will the value be stored as string?

A1:  Yep

Q: when should you use a canvas, and when should you make a blank image using simpleimage?

A1:  There isn't a hard rule, but generally, for drawing, you should use a canvas. When you are doing manipulations of images, then use blank image from simpleimage.

Q: How to you split by tab or space?

A1:  The character \t represents "tab". You can give split on tab with: line.split('\t')

A2:  How to you split by tab or space?

Q: Does the split command turn the string into a list?

A1:  It returns a new list, but yes

Q: does split store things as a list?

A1:  Yes, it stores them in a list

Q: Is there a way to split by a delimeter without removing the delimeter from the output?

A1:  Nope

Q: If you want to an excel file,do you need to split the elements if they are already split in different excel cells?

A1:  So excel files are represented a little differently, but can be exported to a format called CSV (comma separated values) which this sort of code can be used on

Q: Where is parts coming from?

A1:  It’s the return value of the .split function

Q: I thought we need to plot the longitude first… so plot(canvas, long, lat)?

A1:  live answered

Q: Could you explain longitude_to_x?

A1:  live answered

Q: cuz longitude is the x-coordinate?

A1:  live answered

Q: Will there be an opportunity to learn how to export outside of python a game (e.g. heads up) or an animation we create ? Any chance we can make a video or extra session?

A1:  live answered

Q: Why you didn’t have to include the “retunr” command to pass the long and lat to the graph function?

A1:  live answered