Coder Social home page Coder Social logo

Comments (5)

johanlam avatar johanlam commented on May 30, 2024

Responsive is better than fast

from python-random-quote.

github-learning-lab avatar github-learning-lab commented on May 30, 2024

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!

from python-random-quote.

johanlam avatar johanlam commented on May 30, 2024

Keep it logically awesome

from python-random-quote.

github-learning-lab avatar github-learning-lab commented on May 30, 2024

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:

  • git add get-quote.py
  • git commit -m "Random quote bot is working"
  • git push

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

from python-random-quote.

github-learning-lab avatar github-learning-lab commented on May 30, 2024

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.

from python-random-quote.

Related Issues (4)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.