Answered step by step
Verified Expert Solution
Question
1 Approved Answer
write a python coding The parity of a number is the number % 2 and is either 0 or 1. E.g. the parity of 4
write a python coding
The parity of a number is the number % 2 and is either 0 or 1. E.g. the parity of 4 is 0, and the parity of 41 is 1. This question requires 3 separate functions and a main to test them all. One of the most important parts of this question is to use good function naming (and that's one reason why the solution is not provided). In each case, think about what is the most meaningful name you can come up with. Remember: "This function will." Part 1 Write a function to print the parity of a number passed into it (parameter). Write a main function that tests this with the numbers 4 and 41 (which you know should be 0 and 1). Important! Testing is about making sure you know when something is broken! it's useful when testing to actually print the expected results as well as the actual results. The sample below shows what you might do for this question: Parity of 4 should be o: Parity of 41 should be 1: 1 If all you do is call the function, you might see: 1 Did that work? Can you tell? Did that work? Can you tell? It would be better to see something like: Parity of 4 should be 0:1 Parity of 41 should be 1: 0 Oops... something needs fixing! Part 2 Now, in the same program, write a new function that returns the parity of the number you pass into it. Remember to think of a good verb-phrase name for this, but do not use "return" in your name: Since 100% of functions return something, we don't use "return" in names (Functions with no return statement actually implicitly return None.) Test your function similarly to the first one by adding to main Leave your test code for the first version and just add new tests below. Part 3 After you have written and tested the first two functions, write a third function that returns a Boolean (True or False) for whether the number passed into it is odd. Recall from the lecture, that an excellent and common convention for naming Boolean-returning functions is to start their names with ts. Example, the built-in string method isupperly determines if a string is uppercase. We can then use these functions in meaningful, easy- to-read code like the example below. Notice that it is a nice example of concise handling of a Boolean as in our guide. def main(): my_age = int(input ("Age:)) if is_senior(my_age): print("Have a cup of tea") else: print("Please mow the lawn") def is senior(age): return age >= 65 As with the others, you need to test that it works. Write at least two lines in your main program to test. Why test? Because the function has two possible outputs: True or False When you have finished all 3 of these functions, review their names and see how you did. Are the names clear and unambiguous? Would a programmer know how to use these functions based on their names? Note: If you (ever) need to change names that you have used more than once, use PyCharm's excellent refactoring. Don't just edit in multiple places (and maybe forget one) or use find-and-replace (and maybe change something else it matches). Refactoring changes all the names correctly and is quick and safe. Right click on the name, then choose: Refactor > Rename Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started