It is best to use a POGIL approach with the following. In POGIL students work in groups on activities and each member has an assigned role. For more information see https://cspogil.org/Home.
If you work in a group, have only one member of the group fill in the answers on this page. You will be able to share your answers with the group at the bottom of the page.
To read from a file, open the file which returns a file object and loop through the lines in a file. Remember to close the file when you are done reading from it!
Notice that when you run this code it adds a blank line after each line. That is because when you read a line from a file the line contains the newline character (\n). When you print a line you add another newline character which prints as a blank line.
You can also read all the lines from a file at once into a list. One advantage to this is that you can immediately close the file after that. This makes it easier to remember to close the file.
You can also just read some of the lines from a file using the readline method. This can be handy if you just want to see what type of data is in a file, but donβt need to see the entire file.
You can also use with open(file) as name which will automatically close the file after the code block ends. It is particularly important to close the file when you write data to a file.
Create a function, num_chars(filename), that returns the number of total characters (including new lines) in the file with the passed file name. Initialize a count then open the file and loop through all of the lines in the file and add the length of each line to the count. Close the file. Then return the count. There are extra blocks that are not needed in the solution.
def num_chars(filename):
---
count = 0
---
with open(filename) as file:
---
while open(filename) as file: #distractor
---
for line in file:
---
count += len(line)
---
file.close() #distractor
---
return count
Create a function, num_chars(filename), that returns the number of total characters (including new lines) in the file with the passed file name. Initialize a count then open the file and loop through all of the lines in the file and add the length of each line to the count. Close the file. Then return the count. There is an extra block that is not needed in the solution.
Notice how the code above handles the case when the key isnβt yet in the dictionary. The get(key, alternative) method on a dictionary will return the value for the key if it is in the dictionary, otherwise it return return the alternative.
If you worked in a group, you can copy the answers from this page to the other group members. Select the group members below and click the button to share the answers.
<div class="runestone sqcontainer %(optclass)s"> <div data-component="groupsub" id=read_files_groupsub data-size_limit=3> <div class="col-sm-6"> <select id="assignment_group" multiple class="assignment_partner_select" style="width: 100%"> </select> </div> <div id="groupsub_button" class="col-sm-6"> </div> <p>The Submit Group button will submit the answer for each each question on this page for each member of your group. It also logs you as the official group submitter.</p> </div> </div>