I want to print a list of number which is bounded by the range -2 <= x <= 2 in an incremental value such that the list always start with -2 and ends with 2 or vice versa. I'm looking to let the user input an increment value i.e. 0.5 or 0.05. From the range given above it is evident that the largest increment would be 4.
I'm looking to prompt an error message whenever the user inputs an increment value that's > 4 and also when they input an increment value that doesn't allow the list to start printing from 2 and finishes at -2 i.e. when the increment value is 0.23.
The way I thought I could do would be to use the modulus operator where if (4%increment value) = str() i.e. a whole number means that the given increment value is valid for that range. However I'm slightly stuck since when I run my code I end up in a never ending loop. Below is what I've done so far, so hoping somebody can help me out here:
def IncCheck():
while True:
try: #I first check if the user inputs me a string then an error message would come up
c = float(input("For the range -2 <= x <= 2, please input the increment you wish to display: "))
break
except ValueError:
print("Invalid input. The increment has to be a number.")
while 4%c != int(): #Here I wanna check the validity of the increment value so ideally it'd be < 4 and be a value that prints a list that starts with -2 and ends with 2.
print("Your selected increment is not valid for -2 <= x <= 2. It should be a divisible increment of 4(the range). Please try again.")
if c > 4:
print("Your increment is too high. The maximum increment for the given range is 4. Please try again.")
return c
print(IncCheck())
As you can see I've tried to create a function which checks the increment. In the end I'm looking to print the list using something like
import numpy as np
for i in np.arange(2, -2-c, -c): #c is the increment value the users input
print i
which I can use to do other things. I need to use these x values and plot them into an arctan(x) function that I've approximated using a Taylor Series with a N value i.e. the number of iterations in the Taylor Series and compare them side by side with a real arctan(x) function basically. And then graph them. Thanks in advance.