We need to look at one more idea before you race off to build that first game in Python: exception handling.
Syntax errors are a thing of the past, right?
Picture a calculator you wrote. It asks the user for 2 numbers. What happens if the user inputs a number and a letter?
What happens if your code is syntactically correct, but a combination of both programmer and user error breaks it? These events are called exceptions, fixing it is called exception handling.
Python has a list of built-in exceptions. Python also has a simple tutorial that deals with exceptions as well.
A common exception is ValueError. As a budding computer scientist, you should also understand the problem of division by zero. Python handles it with ZeroDivisionError.
exception ValueError
Raised when a built-in operation or function receives an argument that has the right type but an inappropriate value, and the situation is not described by a more precise exception such as IndexError.
exception ZeroDivisionError
Raised when the second argument of a division or modulo operation is zero. The associated value is a string indicating the type of the operands and the operation.
while True:
try:
x = int(input("Please enter a number: "))
break
except ValueError:
print("Oops! That was no valid number. Try again...")
In this example from the Handling Errors chapter in the Python tutorial, an infinite loop is set up. I modified this from the tutorial:
The keyword try indicates that everything with that indentation (starting at try and ending at except) may cause an exception.
If no exception occurs, the except clause (the indentation) is skipped.
If an exception occurs, the rest of the try stops, and Python checks to see if the error matched the one you named. In this case, a ValueError. If the exception matched, the code in the except clause runs.
If everything in step 2 occurs, but the exception does not match, you should generate an unhandled exception ...
while True:
try:
x = int(input('Please enter a number: '))
y = int(input('Please enter a number (Make this a Zero to see the error!!): '))
z = x / y
print(z)
break
except ZeroDivisionError:
print("Oops! That was division by zero!. Try again...")
Exception handling is a necessary component to robust code. Your CPT will require it, and when you start handling files, it will become routine.
We have already worked on individual projects and project management. Your CPT will involve group work. With a partner (only in an actual class, otherwise no partners), write an original application or text-based computer game in Python.
This would be a good time to read 'What makes a Good Programmer'.
You will be given 13 days to:
Have your partner (only in an actual class, otherwise no partners) approved by me before you start to design and code
Choose a partner that wants to build something of the same scope as you want to build.
Do they have the same skill set as you do?
Choose a CA partner that problem solves, codes, and documents like you do.
A friend may not have the same work ethic as you do.
Have your game / application approved by me before you start to design and code
Be aware that the complexity of your game / application has a direct impact on the Knowledge and Thinking categories.
I will explain this to you again when you propose your game / application.
You MUST SIGN OFF ON EACH FUNCTION. You cannot both sign off on the same function (only in an actual class, otherwise no partners).
Write an original application or text-based computer game in Python which must include:
Exception Handling
Functions: Indicate which function you wrote (only in an actual class, otherwise no partners) by placing a comment at the start of the function with your initials. You MUST EACH SIGN OFF ON EVERY FUNCTION. You cannot both sign off on the same function.
Lists
Selection and Repetition Structures
Appropriate Variables Types
Random Numbers
Properly formatted source code
Inline, Off-line and Online Documentation
Games require very clear win / lose conditions
Applications require usable information as output
Only hand in 1 copy of your CA and documentation. Name the files:
Lastname1_Lastname2_CA.py
Lastname1_Lastname2_CA_readme.txt
REMEMBER THAT EVERY FUNCTION MUST BE SIGNED OFF BY ONE OF YOU (only in an actual class, otherwise no partners).
Hangman
Tic-Tac-Toe
Battleship
Risk
Black Jack
21
Crazy 8's
Connect Four
Text based RPG (Linear RPG vs Open Ended RPG)
Roulette
Slot Machine
Craps
Farkle
Yahtzee
Jeopardy / Other Game Shows
Math Quiz
A sport simulator (think Manager or GM Mode)
Pokémon (Or any other build, collect and soup-up 'thing' game)
Snakes and Ladders
Time Based: Type Type Revolution / Auto Racing
An Electronic version of your favourite Board game
Any small application that takes data in, and provides useful information out
Resources:
A great Text to ASCII Generator for making titles - patorjk.com
A great Text to ASCII Generator for making titles - ASCII Generator
A list of Unicode characters that will not crash IDLE - List of Unicode characters at Wikipedia
How to print the Unicode characters:
print('\u2615')
☕
Summative Evaluation
If you can't see the document, please read: You Do Not Need Permission To View Any Documents.
Multiplayer from Replit is a great collaborative tool.
Keep in-mind that your code becomes public.
Back-up your source.
Consider using Replit to COMBINE your code, and code in your school account so that things don't get lost.
The Multiplayer from Replit release trailer.
Replit (Repl.it) introduced Multiplayer. This allowed programers to work together (code, debug, test) at the same time online. Original poster was created by Replit (repl.it), modifications by Sophia F.