Discussion Forum unit 4 CS1102 - python PDF

Title Discussion Forum unit 4 CS1102 - python
Course Programming 1
Institution University of the People
Pages 2
File Size 34.6 KB
File Type PDF
Total Downloads 108
Total Views 200

Summary

great tips for getting on with your discussion and also a better understanding when you are rating your peers...


Description

1.) Precondition violation can happen when there is a wrong condition used in the parameter of a function e.g.; def items(x): # here the wrong condition is the (x) put as a parameter x = (input("please select an item: Chocolate(1) / Sardine(2) / Butter(3) / Milk(4): ")) # here, x is the precondition where it requires a user input. if x == '1' or x == '2' or x == '3' or x == '4' or x == 'Chocolate' or x == 'Sardine' or x == 'Butter' or x == 'Milk': return x[0] else: print ("Not available") items() print("You have chosen item" , items()) Output: >>> *** Remote Interpreter Reinitialized *** Traceback (most recent call last): File "C:\Users\Ralph-Steve\Documents\mine.py", line 22, in print("You have chosen item" , items()) TypeError: items() missing 1 required positional argument: 'x' >>>

2.) A postcondition, must be equal to the value of a precondition for it to give you the desired output e.g.; def items(): x = (input("please select an item: Chocolate(1) / Sardine(2) / Butter(3) / Milk(4): ")) if x == '1' or x == '2' or x == '3' or x == '4' or x == 'Chocolate' or x == 'Sardine' or x == 'Butter' or x == 'Milk': return x[0] else: print ("Not available") items() print("You have chosen item" , items())

Output: >>> *** Remote Interpreter Reinitialized *** please select an item: Chocolate(1) / Sardine(2) / Butter(3) / Milk(4): 2 You have chosen item 2 >>>

3.) When a return value is violated, a function can run but keeps giving an undesired output. This can also mean that the function is not running properly e.g.; def items(): x = (input("please select an item: Chocolate(1) / Sardine(2) / Butter(3) / Milk(4): ")) if x == '1' or x == '2' or x == '3' or x == '4' or x == 'Chocolate' or x == 'Sardine' or x == 'Butter' or x == 'Milk': return [0] # the return value here is supposed to be “return x [0]” else: print ("Not available") items() print("You have chosen item" , items()) Output: >>> *** Remote Interpreter Reinitialized *** please select an item: Chocolate(1) / Sardine(2) / Butter(3) / Milk(4): 1 You have chosen item [0] >>> - Notice that when you enter a number to select an article it will keep saying “You have chosen item [0]”...


Similar Free PDFs