代做Python I代做Python语言

- 首页 >> Python编程

Course Title: Python I

Quiz 2: Classes and Object Oriented Programming in Python

If you want to do use Colab or Jupiter notebook, please submit your .ipynb file with the file name using your student ID

A. Write a Python line that instantiates an object myvehicle of class Vehicle, setting its type attribute to "car" and color attribute to "blue" directly upon creation, without modifying the class definition.

Solution with output:

Hints: Add screenshots of your codes and output here

Sample screenshot of code and results

B. Given a Rectangle class for area calculation, create a Square subclass that inherits from Rectangle but initializes with a single side length. Override the __init__ using super() and add a describe() method in Square to output "Square with side: x", where x is the square's side. Demonstrate by instantiating a Square and calling describe().

Solution with output:

Hints: Add screenshots of your codes and output here

C. In "Breaking Bad," the unique meth formula was crucial to Walter White and Jesse Pinkman's success. Replicate this by securing a secret formula within a Python class SecretFormula, using a private attribute __formula with the value "C10H15N". Implement a class method reveal_formula(cls, passphrase) to disclose the formula only when the correct passphrase "Heisenberg" is used; otherwise, it responds with "Nice try!" Show how to access this formula with both correct and incorrect passphrases. 

Solution with output:

Hints: Add screenshots of your codes and output here 

D. Design a CrossoverEvent data class to monitor a joint event for "Sherlock" and "Doctor Who" characters, including the event name, participants, and outcome. Incorporate type hints and a method to modify the event's result. Initiate the class with "The Time Detective" event, starring "Sherlock Holmes" and "The Doctor," starting with an "In Progress" outcome. Finally, update the status to "Successfully Solved."

Solution with output:

Hints: Add screenshots of your codes and output here

 

 


站长地图