Python Roll Dice Program


You have decided to create an education app that elementary school teachers can use to help teach their students learn addition and subtraction. The app will allow students to virtually roll two dice. The values of the dice will be displayed to the students, and the app will then ask students to input either the sum of the two dice or the difference between the two dice. Students are told whether their answer was correct or not.

This assignment requires several functions with return values. Creating and using these functions is the point of this assignment.

Rounds of play

  • Users can play as many rounds of this game as they want.
  • If a user types “exit” at any time, the program calls a finish() function that outputs a final message and causes the program to end gracefully.

Rolling dice

  • In each round, the value of each die is obtained by calling a function named getDie().
  • This function must return a randomly generated integer value between 1 to 6, inclusive.
  • Your code will call this function twice per round: once for each die.

Asking questions

  • In each round, the program randomly decides whether to ask an addition or subtraction question.
  • The random code that decides which question to ask is centralized in a function called isAdditionQuestion()
  • This function must return a boolean value indicating whether the question should be addition or not.

Grading questions

  • In each round, after the user has answered the question, the program outputs whether the answer was correct or not.
  • The determination about whether a given question was answered correctly or not is made by a function named, isCorrect()
  • This function must accept three arguments: the first die value, the second die value, and a string representing whether the question was an addition or a subtraction question.
  • This function returns a boolean indicating whether the answer was correct or not.

Example

User responses are displayed in bold.

Welcome to the Addition & Subtraction App.

…rolling dice…

You rolled a 5 and a 2.  Please enter the sum of the two dice: 7

Correct!

…rolling dice…

You rolled a 2 and a 3.  Please enter the difference between the two dice: 2

Wrong!

…rolling dice…

You rolled a 1 and a 1.  Snake eyes!  Please enter the difference between the two dice: exit

…exiting…

Thanks for playing.  Bye!

Leave a Reply

Your email address will not be published. Required fields are marked *



  • File Format: Python .py
  • Lines of Code: 58