Code Monkey home page Code Monkey logo

Comments (6)

arpitphillips avatar arpitphillips commented on June 17, 2024

3

from intermediate-python-course.

github-learning-lab avatar github-learning-lab commented on June 17, 2024

Wow, it took 3 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

from intermediate-python-course.

arpitphillips avatar arpitphillips commented on June 17, 2024

next

from intermediate-python-course.

github-learning-lab avatar github-learning-lab commented on June 17, 2024

Wow, it took next 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

from intermediate-python-course.

arpitphillips avatar arpitphillips commented on June 17, 2024

3

from intermediate-python-course.

github-learning-lab avatar github-learning-lab commented on June 17, 2024

Wow, it took 3 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

from intermediate-python-course.

Related Issues (3)

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.