Comments (5)
Responsive is better than fast
from 100-days-of-python.
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 100-days-of-python.
Keep it logically awesome
from 100-days-of-python.
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 100-days-of-python.
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 100-days-of-python.
Related Issues (7)
- Prepare your environment HOT 1
- Run your first Python program HOT 2
- Explore Python on your own HOT 1
- falta que se imprima el resultado de la persona con letraunica HOT 1
- Falta funcion para actualizar lista despues de resultado unico HOT 1
- Falta comprombar los resultados y ordenar y actualizar la lista HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from 100-days-of-python.