arbioeng / intermediate-python-course Goto Github PK
View Code? Open in Web Editor NEWan intermediate Python course
Home Page: https://lab.github.com/everydeveloper/intermediate-python
an intermediate Python course
Home Page: https://lab.github.com/everydeveloper/intermediate-python
All the file is doing right now is printing the sentence "You rolled a die." But the purpose of rolling a die is to come up with a number, so let's do that now.
We can make a new variable named roll
. Variables are simple to make in Python: set the variable equal to what you want it to be. Above the print function, create and set a variable called roll
equal to 5 by writing:
roll = 5
We'll want to print that variable, too, so let's change the print function to reflect what our roll
variable is set to. This is done through Python's f-string
functionality. By putting an f
right before the quotes of the string you're printing, you can then print variables within the string. Let's replace the word 'die' in the sentence to our new roll
variable. This changes the line to say:
print(f'You rolled a {roll}')
Run the code again, and it should say You rolled a 5
. Nice! Now we're getting a number. A die that only rolls 5's isn't useful though, so let's add in the randomness that dice are used for.
Just like a real die, we want the number we roll to be random. We'll need to use a package to do so. Python is full of packages; think of them like tools you can use for specific situations. Just like how you need a shovel to dig a hole, you need the random
package to get a random value in Python.
Right now, you should have a main
function that contains this:
roll = 5
print(f'You rolled a {roll}')
To use a package, we'll need to import it. At the top of the file, import the package random
by writing:
import random
In general, it's good practice to import all packages at the beginning of the script. Now let's use that package to randomize our roll
variable.
To get a random integer with the random
package, we'll update the roll
line like so:
roll = random.randint(1,6)
The random.randint
function takes two integers as parameters. In this case, since we want to emulate a six-sided die, we want to include the range of results that is possible: 1, 2, 3, 4, 5 and 6. Note that the beginning and the end of the range are inclusive bounds. That is, 1 and 6 are possible values, along with integers between them.
Now run the code and put the number you rolled as a comment, and then we'll keep this tutorial rolling!
Welcome to this (mildly) advanced Python tutorial! Today we'll be writing a script to mimic a common real-world action. If you've ever played a tabletop game, you know there are many dice rolls to make. This tutorial will show how to harness a combination of Python skills to make an automatic dice roller. But first, before we do that, we'll need to get a few things set up on your system.
First, in order to do anything in Python, you need to have Python on your computer! Let's make sure it's installed. Open up a terminal and type `python -V. There are a few possible things it can output here:
If the output begins with Python 3
, you're good to go! This tutorial was tested on a system running Python 3.7.4, but it should be compatible with any version of Python 3.
If the output begins with Python 2
, you have Python, but it's an outdated version. You'll need to download Python 3 to follow this tutorial. Go to the Python website to download it.
If you get a command that reads something similar to command not found
, no version of Python is on your system. You'll need to go to the Python website to download it.
We also need to make sure Git is installed on your system. Check that by typing git --version
in a terminal. If it outputs a git version
you're good to go! If not, go to the Git website to download it.
Now that we have Git, we can clone the repository containing the building block of the code you'll be writing. In the terminal type git clone https://github.com/arbioeng/intermediate-python-course
Inside the repo you'll see two files:
README.md
: a markdown file that details some info about the projectdice-roller.py
: a Python file containing the code you'll be building off ofNow that we have everything we need, we can actually begin writing our dice roller! Let's begin!
Leave a comment with your favorite dice-rolling game to continue.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.