diff --git a/flash_cards.py b/flash_cards.py index e69de29..43d3ee3 100644 --- a/flash_cards.py +++ b/flash_cards.py @@ -0,0 +1,49 @@ +import random +from vector2d import Vector2d +def print_conversion(vector, str): + if str == "polar": + print(40 * "-") + print(f"Convert {vector.x}i + {vector.y}j to polar form") + print(40 * "-") + elif str == "rectangular": + print(40 * "-") + print(f"Convert <{abs(vector)}, {vector.angle()}> to rectangular form") + print(40 * "-") + +def print_polar_conversion(vector): + print(40 * "-") + print(f"Convert {vector.x}i + {vector.y}j to polar form") + print(40 * "-") + +def print_rectangular_conversion(vector): + print(40 * "-") + print(f"Convert <{abs(vector)}, {vector.angle()}> to rectangular form") + print(40 * "-") + +def generate_vector(): + random_x = random.randint(-20,21) + random_y = random.randint(-20,21) + return Vector2d(random_x, random_y) + +def print_conversion_solution(vector, str): + if str == "polar": + print(f"<{abs(vector)}, {vector.angle()}>") + elif str == "rectangular": + print(f"{vector.x}i + {vector.y}j") + +def print_polar_conversion_solution(vector): + print(f"<{abs(vector)}, {vector.angle()}>") + +def print_rectangular_conversion_solution(vector): + print(f"{vector.x}i + {vector.y}j") + +if __name__ == '__main__': + while True: + question = input("Would you like to practice polar or rectangular form?") + generated_vector = generate_vector() + if question == "polar": + input(f"{print_polar_conversion(generated_vector)} \n Your answer here ->") + input(f"Correct answer: {print_polar_conversion_solution} \n Press c to continue and press q to quit") + + # But don't forget to break at the right time! + break diff --git a/main.py b/main.py new file mode 100644 index 0000000..a05be25 --- /dev/null +++ b/main.py @@ -0,0 +1,30 @@ +import math +class Vector2d: + def __init__(self, x, y): + self.x = x + self.y = y + def __repr__(self): + return Vector2d(self.x, self.y) + def __str__(self): + return f"{self.x}i + {self.y}j" + def __abs__(self): + c = self.x ** 2 + self.y ** 2 + return c ** (1/2) + def __neg__(self): + return Vector2d(self.x * -1, self.y * -1) + def __add__(self, other): + sum_x = self.x + other.x + sum_y = self.y + other.y + return Vector2d(sum_x,sum_y) + def __eq__(self, other): + if self.x == other.x and self.y == other.y: + return True + def __sub__(self, other): + dif_x = self.x - other.x + dif_y = self.y - other.y + return Vector2d(dif_x, dif_y) + def angle(self): + return math.degrees(math.atan2(self.y, self.x)) + +test = Vector2d(3,4) +test1 = Vector2d(1,1) \ No newline at end of file diff --git a/vector2d.py b/vector2d.py index 083c4ea..ccc7309 100644 --- a/vector2d.py +++ b/vector2d.py @@ -12,7 +12,7 @@ def __str__(self): return f"{self.x}i + {self.y}j" def __abs__(self): - return (self.x ** 2 + self.y ** 2) ** .5 + return round((self.x ** 2 + self.y ** 2) ** .5, 1) def __neg__(self): return Vector2d(-self.x, -self.y) @@ -27,4 +27,6 @@ def __sub__(self, other): return Vector2d(self.x - other.x, self.y - other.y) def angle(self): - return math.degrees(math.atan2(self.y, self.x)) + return round(math.degrees(math.atan2(self.y, self.x)), 1) + +test = Vector2d(3, 4)