Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Conditionals #3

Closed
github-learning-lab bot opened this issue May 22, 2020 · 2 comments
Closed

Adding Conditionals #3

github-learning-lab bot opened this issue May 22, 2020 · 2 comments

Comments

@github-learning-lab
Copy link

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.

@PRUBHTEJ
Copy link
Owner

A total of 5 was rolled

@github-learning-lab
Copy link
Author

Wow, it took A total of 5 was rolled
rolls? 😮 According to my calculations, that is longer than average... Oh well, you don't need good luck if you have python skills! 😎 🐍

Adding User Input

We have a working dice roller right now! It's useful for a very specific situation: rolling two six-sided dice. What if we want to change the number of dice we roll? We can manually edit the code each time, but there's a better way to do this: adding a user input.

Instead of having dice_rolls set to a specific value, let's set it equal to:

int(input('How many dice would you like to roll? '))

Running the code will give a prompt with that question and we can input our desired number of dice. One thing to note: the value a user inputs defaults to a str type and we need it to be a whole number, or an int in Python; that's why we encompass the input command in an int(). Try it out!

Tabletop games use dice with all number of sides so it'll be useful to add a user input for that too. Below where you set dice_rolls add this line:

dice_size = int(input('How many sides are the dice? '))

Change the max value of our roll like this

roll = random.randint(1,dice_size)

When changing a value we had assumed to be a specific number previously, we need to make sure the rest of our code still makes sense. All dice start with one, so our if statement regarding one still makes sense. Our elif statement regarding six doesn't though, as it is no longer guaranteed to be the highest number. Let's make it so our "Critical Success" line is printed to whatever the highest number of our desired dice is. The highest number a dice can roll is the same as the number of sides, so we just to replace our elif statement with:

elif roll == dice_size:

Try it out! Now you can manually input the number of dice and number of sides on the dice knowing that the Critical Success! line will realign accordingly.

Your final main function should look like this:

dice_rolls = int(input('How many dice would you like to roll? '))
dice_size = int(input('How many sides are the dice? '))
dice_sum = 0
for i in range(0,dice_rolls):
  roll = random.randint(1,dice_size)
  dice_sum += roll
  if roll == 1:
    print(f'You rolled a {roll}! Critical Fail')
  elif roll == dice_size:
    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}')

Push your code to GitHub to continue:

git add dice_roller.py
git commit -m "Tutorial complete"
git push

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant