leuchtthurm / intermediate-python-course

an intermediate Python course
https://lab.github.com/everydeveloper/intermediate-python
0 stars 0 forks source link

Using Variables #2

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

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

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!

leuchtthurm commented 3 years ago

6

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

You rolled a 6? πŸ˜„ Nice!

Making Python Roll Multiple Dice

Before moving on, make sure the main function contains:

import random
roll = random.randint(1, 6)
print(f'You rolled a {roll}')

Now, what if we wanted to roll two dice? Well, one way to do that is to run the file twice. Another way is to duplicate the print statement within the file. However, this would get tedious for larger projects, so let's make use of a great Python feature: for loops. A for loop iterates over a range of values and performs a set of commands for all of those values.

How many dice do we want to roll? Let's go with two for now. Make a variable called dice_rolls below import random but above everything else, and set it equal to two.

Now let's make the for loop. Right below where you set dice_rolls type:

for i in range(0,dice_rolls):

Let's unpack what this means. If we were to make this a grammatical sentence it would read something like this:

"for every index in a range starting after 0 until the value is equal to dice_rolls, do this:"

We would then have a set of commands it would do for every i. We already have the commands we want to be repeated, we just have to tell Python that by indenting them. Once they're indented, save the file and run it to get the two rolls.

Manipulating Variables

With a for loop implemented, your main function takes this form:

dice_rolls = 2
for i in range(0,dice_rolls):
  roll = random.randint(1,6)
  print(f'You rolled a {roll}')

In most game situations, rolling multiple dice is followed by adding their values together. Let's do that now. In Python, arithmetic can be performed on variables just by using the equivalent mathematical symbols. Want to add two variables together? It's as simple as: variable_sum = variable_1 + variable_2. Since our dice values are stored in a single updating variable instead of separate variables, however, we'll have to add the values upon themselves. First, make a variable called dice_sum above the for loop, and set it equal to 0:

dice_sum = 0

We'll be adding each value of dice_roll we get to this dice_sum variable as we loop through each dice roll. In the for loop, right after the roll variable is assigned, type:

dice_sum = dice_sum + roll

Another way we can do this in Python is using the += operator:

dice_sum += roll

It doesn't matter which version you prefer, just go with whichever way is easier for you to visualize. Finally, let's print out our final value for dice_sum. After the for loop is finished, type:

print(f'You have rolled a total of {dice_sum}')

Make sure this statement is not indented (since that would include it in the for loop). Overall, your main function should look like this:

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

If you save and run the file, you'll now get the values of the two dice rolls and their combined sum. Now our code is developing some complexity!

Push your code to GitHub to continue:

git add dice_roller.py
git commit -m "Now rolling two dice"
git push
github-learning-lab[bot] commented 3 years ago

🎲🎲 Snake Eyes! 🐍

Click here to learn how to add some custom responses!