adamd / python-random-quote

A file-based quote bot written in Python
https://lab.github.com/everydeveloper/introduction-to-python
0 stars 0 forks source link

Read from a file #3

Closed github-learning-lab[bot] closed 5 years ago

github-learning-lab[bot] commented 5 years ago

Now we're ready to really build our quote bot. To test things out, we'll read all the quotes from a file and print the first one.

First, you can remove our test quote, the print statement on line 2. You can either comment it out by adding a # at the start of the line, or remove it completely.

Next, remove the other comments by deleting # from the start of the other four lines to get Python code like this:

  f = open("quotes.txt")
  quotes = f.readlines()
  f.close()

  print quotes

Here we are opening the quotes.txt file, reading all the lines into a new variable called quotes, then closing the file (defined by the variable f). Finally, we print out the quotes.

You can run this code and we'll get a dump of all the quotes in the quotes file. That's because Python stored them all in an array, which is a single variable that holds a list of values.

Print the first element of an array

Since we only want one quote, we need to edit our code to print only the first value in the quotes array.

In your code, find the print line and add this special modifier [0] so that the line now reads: print quotes[0]

The square brackets tell Python that we want a specific item in the array. Since it starts counting at zero, we've grabbed the first item.

Comment with the first quote

Run your code and copy the value to your clipboard.

Paste the quote as a comment here and I'll follow up with next steps!

adamd commented 5 years ago

Responsive is better than fast

github-learning-lab[bot] commented 5 years ago

Great! That's the first line. Now, how would we print the last line?

You'll want to edit [0] to include the index of the last value in the array. You can check out quotes.txt to see the number of lines.

Since the first value is the zeroth, you'll need to remove one from the number you include for the last value.

Comment with the last quote

Run your code and copy the value to your clipboard.

Paste the quote as a comment here and I'll follow up with next steps!

adamd commented 5 years ago

Keep it logically awesome

github-learning-lab[bot] commented 5 years ago

Now that you're familiar with arrays, it's time to find a random line.

Generate a random number

Rather than [0] or [13] for our array value, we want the number to be chosen at random. Of course, it can't be any number. It should be a whole number between 0 and 13.

To generate random numbers, we'll use a Python module, a built-in extension of the language.

At the very top of your get-quote.py file (above the def line), add this line: import random

Now find where you print out one value from the array. Before that line, add the following:

  last = 13
  rnd = random.randint(0, last)

The last variable holds the highest index for the array. Then our random number is stored in rnd using the random.randint function, which takes the lowest-possible number (zero) and the highest-possible number (stored in last).

Note If you want to add or remove quotes from your text file, you could change the last variable to update automatically: last = len(quotes) - 1

Finally, update the line where we print a single quote. Instead of including a number between the brackets, we'll put our random number variable: print quotes[rnd]

Try running your code a few times and your quotes should now be chosen at random.

Push your changes

It's been awhile since we've saved our code in the repo. Take the oppportunity now:

When I see the push come through, I'll comment here!

github-learning-lab[bot] commented 5 years ago

You have a working random quote bot! You've finished this course, but the learning never stops. I've created one last issue for you to explore Python on your own.