Giter Club home page Giter Club logo

intermediate-python-course's People

Contributors

grobergm avatar iberiaz9 avatar

Watchers

 avatar  avatar

intermediate-python-course's Issues

Using Variables

Printing Variables

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.

Randomizing Variables

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!

Adding Conditionals

Let's add some flavor to our responses, dependent on how we roll. We can do this by using conditionals. We'll put a command that only triggers if a certain condition is met. For example, let's add "Critical Fail" in the printed statement if the die roll is a one.

In Python, we can set this as the criteria by replacing our print statement in the loop with:

if roll == 1:
  print(f'You rolled a {roll}! Critical Fail')

But what if it's not a one? We need a catch-all for all the other conditions it could be. This is done in Python with the else command. Below our if statement and the related print function, add an else statement:

else:
  print(f'You rolled a {roll}')

Now we have two different statements that can be printed. Try running your code until you have a roll that is a one and a roll that is something else.

If we have additional conditions we want to add, we can use a third type of conditional in Python: elif. This is used for a specific condition after if has been used. Try adding this elif statement between the if and else statements:

elif roll == 6:
  print(f'You rolled a {roll}! Critical Success!')

With the conditionals added, your main function should look something like this:

dice_rolls = 2
dice_sum = 0
for i in range(0,dice_rolls):
  roll = random.randint(1,6)
  dice_sum += roll
  if roll == 1:
    print(f'You rolled a {roll}! Critical Fail')
  elif roll == 6:
    print(f'You rolled a {roll}! Critical Success!')
  else:
    print(f'You rolled a {roll}')
print(f'You have rolled a total of {dice_sum}')

Leave a comment with the number of rolls it took to get a Critical Success.

Set up your project

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.

Having Python

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.

Having Git

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.

Cloning this Repository

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/iberiaz9/intermediate-python-course

Inside the repo you'll see two files:

  • README.md: a markdown file that details some info about the project
  • dice-roller.py: a Python file containing the code you'll be building off of

Now 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.

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.