Last active
December 9, 2021 02:55
-
-
Save misterhtmlcss/4db08e281c4800b6b2c3478d83ddab4a to your computer and use it in GitHub Desktop.
Define precondition and postcondition show return errors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# CS1101 - Discussion Forum Unit 4 | |
# Section 6.9 of your textbook ("Debugging") lists three possibilities to consider if a function is not working. | |
# ✓ Define "precondition" and "postcondition" as part of your description. (First two paragraphs) | |
# ✓ Describe each possibility in your own words. (All three paragraphs) | |
# A precondtion is a problem before the function’s arguments are passed into the parameters (Downey, A. 2015). Are there arguments being passed? Sometimes a function is called a long ways away from where it’s defined. With languages like Python it’s often critical to name parameters effectively so that the correct arguments are passed. This can be as simple as missing a type e.g. ‘5’ instead of 5. | |
# A postcondition is a problem that follows from the point immediately following the arguments being passed (Downey, A. 2015). A really simple example could be `function (a, b) { b -a }`. No matter the language we can all see how it’s unlikely that a human mind would order the parameters as a followed by b and then have them flipped within the function body. Another example could be variable mutation is like a disease. Sometimes you instantiate a variable a particular way within a function, but it is mutated without your knowledge and this impacts a possibly critical sequence of events. A set of examples of this can be found at Python Tips (2017) | |
# Another problem is the ‘moment’ of computation return. The moment when we use the value created from the function can trip people up, especially new programmers that are used to using print statements. Often new programmers will have a print statement where a return statement is necessary. Without a return statement there is no value being returned and this can lead to a lot of confusion since a return statement doesn’t give any signal on a REPL program. Additionally a huge weak point and I saw this today was a failure to use returns within an if/else conditional workflow. Due to the missing return the function was also applying a final statement on the last line that wasn’t meant to be called. In fact the developer left this statement only as a reminder for something else to do, but it was in fact messing up the rest of his program. | |
# Reference | |
# Downey, A. (2015). Think Python: How to think like a computer scientist. Green Tea Press. | |
# Ullah, M. Y. Khalid (2017). Python Tips. Retrieved from https://book.pythontips.com/en/latest/mutation.html | |
# FOR SPACING ---- Ignore me | |
print("\n\n") | |
# FOR SPACING ---- Ignore me | |
# ✓ Create your own example of each possibility in Python code. List the code for each example, along with sample output from trying to run it. | |
store={} | |
# List (Python) of Dictionaries or Objects (JavaScript terminology) | |
todos=[] | |
current_list="" | |
# Task should be a string and isDone is a boolean defaulting to false. | |
def add_todo(task, isDone=False): | |
# Is task a String | |
if isinstance(task, str): | |
# String and Bool in a Dictionary (Python) / Object (JavaScript) | |
todo={ 'task': task, 'complete': isDone} | |
# Dictionary appended to 'todos' List | |
todos.append(todo) | |
# Test the todos was updated | |
# print('todos1', todos) | |
# Return value of todos | |
return todos | |
else: | |
# Test out put on failure | |
# print("Empty a todo.", task) | |
return "Empty a todo." | |
# Precondition ------ problem; this is at the argument stage and before it's passed into the parameters | |
add_todo(True, "Tomatos") | |
add_todo("Lettuce", False) | |
# Postcondition ------ Function call sends string argument, but 'if' condition doesn't catch this error and passes the data to the store. See how the store has an empty string now? | |
add_todo("", False) | |
# Test global has been correctly mutated according to plan. | |
print("todos", todos, "\n\n") | |
# ---------------- I wrote a bunch of prints because of the below stretch..I left them in to see what it took for me to get it working ---------------- | |
# This function takes a list of task that the user generates and then addes them to a specific label e.g. Recipies, Groceries | |
# fn=function, task=string, isDone=Boolean, label=string | |
def todo_lists(fn, task, label, current_list): | |
# print('first current list', current_list) | |
if current_list == label: | |
store[label] = fn(task) | |
# print('store', store) | |
# Remove any of these current_lists and most of the functionality crashes, but without the user knowing immediately. | |
return current_list | |
elif not current_list: | |
current_list = label | |
# print('current list', current_list) | |
store[label] = fn(task) | |
# print('store', store) | |
# Remove any of these current_lists and most of the functionality crashes, but without the user knowing immediately. | |
return current_list | |
else: | |
current_list = label | |
todos.clear() | |
# print("todos cleared", todos) | |
store[label] = fn(task) | |
# print('store', store) | |
# Remove any of these current_lists and most of the functionality crashes, but without the user knowing immediately. | |
return current_list | |
todo_lists(add_todo, "M&Ms", "Grocery List", False ) | |
print("\n\n") | |
# ************************** | |
# *************** Start of assignment silliness *************** | |
# Interested in more? I had some fun for a bit. Wanted to try push what I'm learning through class, the book and elsewhere. | |
# The below is way way off base, but it was fun and I just went for it. I left ALL the prints in there, so I'd recommend removing them and reading it and then adding them back in and reading and watching what happens... | |
# ************************** | |
def run_program(current_list, count=0): | |
counter = int(count) | |
if counter <= 0: | |
print('lists', store) | |
print('done running the program') | |
return | |
else: | |
my_todo=input("What do you need today? ") | |
print("\n") | |
my_label=input("What list would you like to add this to? ") | |
print("\n") | |
prev_list=todo_lists(add_todo, my_todo, my_label, current_list) | |
# print('prev_list', prev_list) | |
run_program(prev_list, counter-1) | |
if __name__ == "__main__": | |
run_program(current_list, input("Let's create some todo lists today!! How many would you like to create? ")) | |
# ---------------- End of assignment silliness ---------------- | |
# This is the entrypoint function. This is called on file run. When run it prints all the data and in the order required by the instructions. |
Thanks
@Daniel-Nim may I ask how and why you are finding my exercises? I'm just curious.
I am finding or accessing your exercises via Google search. I did create an account with you and I use the account to ask questions and get answer. I am finding it out because I have need for those practice.
"those practices". I think having a dual
Nice
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks