Uploaded by Kevin Ly

Homework #4

advertisement
Kevin Ly
ME30
Professor Bhaskar
November 22, 2023
Homework #4
11.3-11: Problem 5)
class Count:
#init method
def __init__(self, time):
self.time = time
def increment(self, second):
try:
if self.time + second < 0:
raise Exception()
return self.time+second
except:
print("Time will be negative and bigger value than the object time")
return self.time;
x = Count(100)
print("Time after increment is", x.increment(5))
print("Time after increment is", x.increment(-50))
print("Time after increment is", x.increment(-101))
print("Time after increment is", x.increment(-100))
Output:
Time after increment is 105
Time after increment is 50
Time will be negative and bigger value than the object time
Time after increment is 100
Time after increment is 0
11.3-11: Problem 6)
Yes, I think that physical time could be negative and we see it in sports a lot. For example, in a
track and field race, the referee has to count from 3, 2, 1, and “go”. The stopwatch starts at “go”
but the referee had to count down from 3, 2, and 1. This is an example of negative physical time.
11.4-10: Problem 1)
class faceCard:
def __cmp__(self, card):
c1 = self.suit, self.rank
c2 = card.suit, card.rank
return cmp(c1, c2)
3) Write a Python Program using turtles package to draw a (a) nonagon:
import turtle
def polygon(side, length):
angle = 360 / side
for _ in range(side):
turtle.forward(length)
turtle.left(angle)
def main():
turtle.speed(2)
#Drawing for nonagon
turtle.color("blue")
polygon(9,50)
#New position for decagon
turtle.penup()
turtle.goto(120,0)
turtle.pendown()
#Drawing for decagon
turtle.color("green")
polygon(10,50)
turtle.done()
if __name__ == "__main__":
main()
Download