Post on 16-Mar-2018
transcript
Robbie Coyne A453 Task 1
A453 Task 1:
Analysis:
Problem:
The problem I need to solve is that I need to design, code, and test a program that simulates a dice throw of a 4, 6, or 12 sided die and outputs the result before repeating the process again for as many times as desired by the user.
Solution:
I can solve this problem by identifying the steps needed for a program to perform this process and then programming those steps in Python 3.
Success Criteria:
The success criteria of this program would that:
• The program would have the user able to input which of the dice is thrown.
• The program must generate a random number between 1 and the user’s input.
• The program output the amount of sides on the die and the number it has landed on.
• The user able to repeat the process as many times as they want.
• The program able to simulate a 4 sided die.
• The program able to simulate a 6 sided die.
• The program able to simulate a 12 sided die.
• The program able to identify incorrect die-number input.
Testing:
I'll test the program by running it and giving different inputs when asked what numbered die I’d like to select and whether to see if the expected outcomes are correct and then putting the results in a testing table with a column explaining what I'm going to do, then a column explaining the expected outcome, and a third column explaining the actual outcome.
1
Robbie Coyne A453 Task 1
Design:
Variables:
1.continued: This is a boolean variable that is initially set to true. The rest of the code is contained in a while loop that will repeat until continued is set to false. It is set to false when the user decides not to continue when asked by the program.
2.sides: This is a string variable that is set to the users' input when the program asks them how many sides they want the die to have.
3.sidesint: This is an integer variable that is set to an integer form of the value of the “sides” variable.
4.result: This is an integer variable that is set to a random number between 1 and the maximum number possible of the chosen die.
Functions:
1. randint: a function stored in the random class that has 2 integer variable inputs. The function outputs a random number between these 2 numbers.
2. print: a function that outputs a given string value.
3. input: a function that returns a string value from the user’s input after it outputs a given string value.
4. lower: a function that returns the value of a given string but with all letters being lowercase.
5. int: a function that returns the value of a given value that has been converted to an integer.
2
Robbie Coyne A453 Task 1
3
Flow Chart:
Robbie Coyne A453 Task 1
Validation:Validation occurs when the user is asked which of the dice to pick as any input that isn't digits of either 4, 6, or 12 is
disregarded and the user is asked for the input again. It also occurs when the user is asked whether to continue or not as any input that isn't text that says “yes” or “no” is disregarded and the user is asked for the input again.
4
Robbie Coyne A453 Task 1
Draft Code solutions:
First Coding attempt:#Dice simulator for a 4,6 and 12 sided die
print("Welcome to my die simulator")
import random
loop = 23
while loop != 22:
sides = input("What sided dice do you want to roll a 4,6 or 12 sided die?")
if sides=="4":
result=random.randint(1,4)
print("The number that the dice has landed on is",result)
elif sides=="6":
result=random.randint(1,6)
print("You rolled a 12 sided dice and the number that the dice has landed on is",result)
elif sides=="12":
result=random.randint(1,12)
print("You rolled a 12 sided dice and the number that the dice has landed on is",result)
else:
print("I’m sorry, you can only roll a 4,6 or 12 sided dice in this game")
This attempt was not acceptable as if the user could enter a letter instead of a number and cause IDLE to throw an exception and the user could also enter a different number to the ones that the program accepts. Also, it doesn’t allow the user to keep on rolling the die as many times as they desire.
5
Robbie Coyne A453 Task 1
Second Coding attempt:#Dice simulator for a 4,6 and 12 sided die
print("Welcome to my die simulator")
import random
continued = True
while continued == True:
sides = input("What sided die do you want to roll, a 4,6 or 12 sided die?")
sidesint = int(sides)
while sidesint != 4 and sidesint != 6 and sidesint != 12:
sides = input("I'll ask you again! What sided die do you want to roll, a 4, 6 or 12 sided die?")
if sidesint == 4:
result=random.randint(1,4)
print("The number that the dice has landed on is",result)
elif sidesint == 6:
result=random.randint(1,6)
print("You rolled a 12 sided dice and the number that the dice has landed on is",result)
elif sidesint == 12:
result=random.randint(1,12)
print("You rolled a 12 sided dice and the number that the dice has landed on is",result)
else:
print("I’m sorry, you can only roll a 4,6 or 12 sided dice in this game")
check = input("Would you like to continue? Yes or no!")
check = check.lower()
while check != "yes" and check != "no":
check = input("I'll ask you again! Would you like to continue? Yes or no!")
if check == "no":
continued = False
print("Ahh well!")
else:
print("Yay")
This attempt was not acceptable as although I’ve added in code to loop the program as long as the user desires (on line 5 I have started a while loop that will continue as long as the value of “continued” is True, and from line 21 onwards, I’ve
6
Robbie Coyne A453 Task 1
asked the user whether they want to continue and then set the value of “continued” depending on that decision) and I’ve added code to make sure that the user can’t input an incorrect number (between lines 6 and 9, I’ve asked the user what numbered die they would like to roll and if the input isn’t . It still doesn’t deal with the possibility of the user not
inputting numbers.
Final Code:#Dice simulator for a 4,6 and 12 sided die
print("Welcome to my die simulator")
#Show the text "Welcome to my die simulator"
import random
#import all resources (functions) from random library
continued = True
#Create a boolean variable called "continued" and set its' value to True.
while continued:
#Loop following indented code as long as the value of continued is True.
sides = input("What sided die do you want to roll, a 4,6 or 12 sided die?")
#Create a string variable called "sides" and set the value to the users' input after the text "What sided die do you want to roll, a 4, 6, or 12 sided die?"
try:
#Attempt the following indented code.
sidesint = int(sides)
#Create an integer variable called "sidesint" and set its' value to the value of "sides" converted to integer.
except:
#If the code i nside the try statement fails, carry out the following indented code instead.
sidesint = 0
#Create an integer variable called sidesint and set its' value to 0.
while sidesint != 4 and sidesint != 6 and sidesint != 12:
#Loop following indented code as long as the value of sidesint os not equal to 4, 6, or 12.
sides = input("I'll ask you again! What sided die do you want to roll, a 4, 6 or 12 sided die?")
#Set the value of "sides" to the users' input after the text "I'll ask you again! What s ided die do you want to roll, a 4, 6, or 12 sided die?".
try:
#Attempt the following indented code.
sidesint = int(sides)
#Set the value of "sidesint" to the value of “sides” as an integer.
except:
#If the code in the try statement fails, carry out the following indented code instead.
7
Robbie Coyne A453 Task 1
sidesint = 0
#Set the value of "sidesint" to 0.
if sidesint == 4:
#If the value of sidesint is 4, carry out the following indented code.
result=random.randint(1,4)
#Create an integer variable called “result” and set its’ value to a random number between 1 and 4.
print("You rolled a 4 sided die and the number that the die has landed on is",result)
#Show the text “You rolled a 4 sided die and the numbe r that the die has landed on is” followed by the value of “result”.
elif sidesint == 6:
#If the previous “if” statement’s parameters are not met and the value of “sidesint” is 6, carry out the following indented code:
result=random.randint(1,6)
#Crea te an integer variable called “result” and set its’ value to a random number between 1 and 6.
print("You rolled a 6 sided die and the number that the die has landed on is",result)
#Show the text “You rolled a 6 sided die and the number that the die has l anded on is” followed by the value of “result”.
elif sidesint == 12:
#If the previous “if” statements’ parameters are not met and the value of “sidesint” is 12, carry out the following indented code:
result=random.randint(1,12)
#Create an integer var iable called “result” and set its’ value to a random number between 1 and 12.
print("You rolled a 12 sided die and the number that the dice has landed on is", result)
#Show the text “You rolled a 12 sided di e and the number that the die has landed on is” followed by the value of “result”.
else:
#If the previous “if” statements’ parameters are not met, carry out the following code:
print("I’m sorry, you can only roll a 4, 6 or 12 sided die in this game")
#Show the text “I’m sorry, you can only ro ll a 4, 6, or 12 sided die in this game”
check = input("Would you like to continue? Yes or no!")
#Create a string variable called “check” and set its value to the user’s input when asked “Would you like to continue? Yes or no!”
check = check.lower()
#C hange the value of “check” so that all its letters are lowercase.
while check != "yes" and check != "no":
#Loop following indented code as long as the value of “check” is not equal to “yes” or “no”
check = input("I'll ask you again! Would you like to continue? Yes or no!")
#Create a string variable called “check” and set its value to the user’s input when asked “I’ll ask you again! Would you like to continue? Yes or no!”
8
Robbie Coyne A453 Task 1
if check == "no":
#If the value of “check” is equal to “no”, carry out the follow ing indented code:
continued = False
#Set the value of “continued” to False.
print("Ahh well!")
#Show the text “Ahh well!”
else:
#If the previous “if” statement’s parameters are not met, carry out the following code:
print("Yay")
#Show the te xt “Yay”
Testing:
Testing Table:Test
number:Description: Test
data:Technique: Expected
Result:Actual Result:
1 Test to see if the 4 sided die works by choosing it when
prompted.
4 When asked “What sided die do you want to roll, a 4, 6 or 12 sided die?” I’ll input the test data (which
is 4).
The program should output
text saying “You rolled a 4 sided
die and the number the die
landed on is” followed by a
random number
between 1 and 4.
The program has outputted text that says “You rolled a 4 sided
die ant the number that the die landed on is 4” which is just as predicted,
with the ending “4” being a
random number between 1 and 4
9
Robbie Coyne A453 Task 1
2 Test to see if the 6 sided die works by choosing it when
prompted.
6 When asked “What sided die do you want to roll, a 4, 6 or 12 sided die?” I’ll input the test data (which
is 6).
The program should output
text saying “You rolled a 6 sided
die and the number the die landed on is “ followed by a
random number
between 1 and 6.
The program outputted text that says “You rolled a 6 sided
die and the number that the die has landed
on is 5” which is just as predicted, with the ending
“5” being a random number between 1 and 6.
3 Test to see if the 12 sided die works by choosing it when
prompted.
12 When asked “What sided die do you want to roll, a 4, 6, or 12 sided die?” I’ll input the test data (which
is 12).
The program should output
text saying “You rolled a 12
sided and the number the die
landed on is “followed by a
random number
between 1 and 12.
The program outputted text that says “You
rolled a 12 sided die and the
number that the die landed on is 2” which is just as predicted,
with the ending “2” being a
random number between 1 and
12.4 Test to see if the program
correctly identifies an incorrect number of sides for
a dice.
3 When asked “What sided die do you want to roll, a 4, 6, or 12 sided die?” I’ll input the test data (which
is 3).
The program should output text saying “I'll ask you again! What sided die do you want to roll, a 4, 6, or 12
sided die?” leaving the user
free to try again.
The program outputted text saying “I'll ask
you again! What sided die do you want to roll, a 4,
6, or 12 sided die?” which is
just as predicted as the user was then allowed to make another
attempt.5 Test to see if the program
correctly identifies incorrect value input in the die number
section.
“hello” When asked “What sided die do you want to roll, a 4, 6, or 12 sided die?” I’ll input the test data (which
is “hello”).
The program should output text saying “I'll ask you again! What sided die do you want to roll, a 4, 6, or 12
sided die?”
The program outputted text saying “I'll ask
you again! What sided die do you want to roll, a 4,
6, or 12 sided die?” which is
10
Robbie Coyne A453 Task 1
leaving the user free to try
again.
just as predicted as the user was then allowed to make another
attempt.6 Test to see if the user can
repeat the process as many times as required.
“yes” When asked “Would you like to continue? Yes or no!” I’ll input the
test data (which is “yes”).
The program should output
text saying “Yay” and then
ask the user “What sided die do you want to roll, a 4, 6, or 12
sided die?
The program outputted text
saying “Yay” and then “What
sided die do you want to roll, a 4,
6, or 12 sided die?” which is
just as predicted.7 Test to see if user can
choose not to repeat the process so that they can quit the program.
“no” When asked “Would you like to continue? Yes or no!” I’ll input the
test data (which is “no”).
The program should output
text saying “Ahh well!”
before it stops.
The program outputted text
saying “Ahh well!” before it
stopped which is just as predicted.
8 Test to see if the program correctly identifies incorrect
input in the continue section.
“hello” When asked “Would you like to continue? Yes or no!” I’ll input the
test data (which is “hello”).
The program should output text saying “I'll ask you again! Would you like to continue? Yes or no!” before the user has another attempt at giving an input.
The program outputted text saying “I'll ask
you again! Would you like
to continue? Yes or no!”
Evidence:1.
11
Robbie Coyne A453 Task 1
2.
3.
4.
5.
12
Robbie Coyne A453 Task 1
6.
7.
8.
Evaluation:
I believe this code is successful because it follows all the success criteria that I explained in the Analysis. I said that:
13
Robbie Coyne A453 Task 1
• “The program would have the user able to input which of the dice is thrown”. Which I’ve done by having an integer variable called “sidesint” that is set to the integer conversion of the user’s input when asked "What sided die do you want to roll, a 4, 6 or 12 sided die?”
• “The program must generate a random number between 1 and the user’s input”. Which I’ve done by having “if” statements which are run depending on the value of “sidesint” and inside each of these “if” statements is an integer variable called “result” that’s set to a random number between 1 and whatever the user’s input happened to be.
• “The program output the amount of sides on the die and the number it has landed on”. Which I’ve done by having the “print” function called and given the string input of “You rolled a “plus the number of sides on the dice plus “and the number that the dice has landed on is” plus the value of “result”.
• “The user able to repeat the process as many times as they want”. Which I’ve done by creating a string variable that’s set to the user’s input when asked "Would you like to continue? Yes or no!" This input is then used to decide whether or not the value of “continued” is set to true or false, and if it is set to false, then the program will stop, but if it is set to true then the program will repeat the process.
• “The program able to simulate a 4 sided die”. Which I’ve done by having it so that if the user inputs a 4 when asked "What sided die do you want to roll, a 4, 6 or 12 sided die?”, an integer variable called “result” will be set to a random number between 1 and 4. The value of “result” will then be outputted along with the number of sides of dice which in this case is 4.
• “The program able to simulate a 6 sided die”. Which I’ve done by having it so that if the user inputs a 6 when asked "What sided die do you want to roll, a 4, 6 or 12 sided die?”, an integer variable called “result” will be set to a random number between 1 and 6. The value of “result” will then be outputted along with the number of sides of dice which in this case is 6.
• “The program able to simulate a 12 sided die”. Which I’ve done by having it so that if the user inputs a 12 when asked "What sided die do you want to roll, a 4, 6 or 12 sided die?”, an integer variable called “result” will be set to a random number between 1 and 12. The value of “result” will then be outputted along with the number of sides of dice which in this case is 12.
• “The program able to identify incorrect die-number input”. Which I’ve done firstly by having an exception when setting the value of “sidesint” to the integer conversion of the user’s input. So that if the user’s input isn’t in digits, the program will remain running rather than crash and will instead set the value of “sidesint” to 0. Secondly, I’ve put a while statement that will continue to ask for the user’s input until it is either 4, 6 or 12.
14
Robbie Coyne A453 Task 1
My solution is good because it meets all the success criteria and efficiently deals with invalid user input. Also, it doesn’t allow the user to continue until they’ve given a valid response, while other programs would simply stop the process and simply assume that the user has decided to quit, which could be annoying if done accidentally.
My solution is bad because it is very inefficient and doesn’t use functions to increase efficiency and the ability to read and understand the code. Also, my program is bad because it doesn’t allow the user to quit until the process is complete.
The easiest part of the coding was figuring out how to get the program to repeat as many times as required as all this involved was creating a while loop that depended on a boolean variable having a value of true.
The hardest part of the coding was deciding how to deal with characters being inputted by the user when they’re supposed to enter digits. I solved this by finding out about the existence of exceptions and making use of them in this scenario.
I could improve my solution by adding a function to “roll the dice” that takes a parameter for the amount of sides, rather than having a repeated section as I’ve done. Also, I could add in a predetermined answer into the question that asks how many sides of dice that quits the program early.
15