14: Reading data from a file in python

Extend it:

Use and store data in files
Reading data from a file in python

For this challenge you will read parts of a story from a file and use them to generate a random story.

The starting point for your code reads data from a file called words.csv and starts off your random story:

Challenges:

 
  1. Make a variable called food and set it to a random thing to eat that you’ve read from the file

    Hint: Use random.choice to choose a value from the list called foods which contains all the different foods read from line 4 of words.csv

  2. Add a new line to the file with things that might happen suddenly (e.g. met a dinosaur, fell in a hole, needed the loo)

    Hint: Remember that a csv file is just a text file with all values separated by commas , and new lines

  3. Make a variable called suddenly and use random.choice to choose what suddenly happens next

    Hint: You’ll also need to use readline and split to get the data from the CSV file

Leave a Reply

Your email address will not be published. Required fields are marked *