Created
November 6, 2021 22:11
-
-
Save ElnuDev/49e36014acae4d403a263d119df649b8 to your computer and use it in GitHub Desktop.
Slope field calculator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from math import nan | |
def slope(x, y, equation): | |
try: | |
# Edit this to match the given dy/dx in the problem | |
return eval(equation) | |
except ZeroDivisionError: | |
# Return NaN (Not a Number) for division by zero | |
return nan | |
def integer_input(prompt, suffix = ": "): | |
while True: | |
try: | |
value = int(input(prompt + suffix)) | |
break | |
except: | |
print("Please input an integer.") | |
return value | |
def equation_input(prompt, suffix = ": "): | |
while True: | |
try: | |
value = input(prompt + suffix) | |
# set some arbitrary testing x- and y-values to see if the equation is valid | |
slope(0, 0, value) | |
break | |
except: | |
print("Please input a valid equation in terms of x and y.") | |
return value | |
slope_equation = equation_input("dy/dx") | |
min_x = integer_input("min x") | |
max_x = integer_input("max x") | |
min_y = integer_input("min y") | |
max_y = integer_input("max y") | |
for x in range(min_x, max_x + 1): | |
print() | |
for y in range(min_y, max_y + 1): | |
print(f"({x}, {y}): {round(slope(x, y, slope_equation), 2)}") | |
if x < max_x: | |
input("Press enter for next range of x-values") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment