-
Notifications
You must be signed in to change notification settings - Fork 0
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
Comments
A total of 5 was rolled |
Wow, it took A total of 5 was rolled Adding User InputWe 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 Instead of having 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 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_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 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 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:
|
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:
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 ourif
statement and the related print function, add anelse
statement: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 afterif
has been used. Try adding thiselif
statement between theif
andelse
statements:With the conditionals added, your main function should look something like this:
Leave a comment with the number of rolls it took to get a Critical Success.
The text was updated successfully, but these errors were encountered: