0

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.

martineau
  • 119,623
  • 25
  • 170
  • 301
user3613025
  • 373
  • 3
  • 10
  • So do you want to start at `2` or `-2`? Or do you want to determine where to start depending on what the user entered? i.e. if negative, start at `2`, otherwise start at `-2`? – smac89 Nov 13 '17 at 03:08
  • Have you checked your condition, `4%c != int()`, in the shell to see how it behaves? Does it do what you think? You never change `c` and you don't have a `break` statement. – wwii Nov 13 '17 at 03:10
  • [Ask a User for input till they give a valid response](https://stackoverflow.com/a/23294659/2823755). – wwii Nov 13 '17 at 03:14
  • @smac89 It doesn't matter whether I start with 2 or -2 as long as the list starts and ends with either 2 or -2 it'll be what I need. – user3613025 Nov 13 '17 at 16:21

2 Answers2

0

let increment be c.

  • if c is less than 0, check
    • find X by evaluating: 2 + (cX) = -2
    • Check X by plugging it into the above equation and verify result
  • else if c is greater than 0, check
    • find X by evaluating: -2 + (cX) = 2
    • Check X by plugging it into the above equation and verify result
  • else, not possible - cannot go anywhere with an increment of zero. Throw an exception or print error message
smac89
  • 39,374
  • 15
  • 132
  • 179
0

This is how I would do it (without the user input routine)

def increment_(c, start = -2, end = 2):
    assert np.isclose((end - start) % c, 0), "Step does not evenly divide range"
    steps = (end - start) / c
    if np.signbit(steps):
        return np.linspace(end,   start, - int(steps) + 1)
    else:
        return np.linspace(start, end,     int(steps) + 1)

The infinite loop in your user input is explained in this question which @wwii linked in the comments

Daniel F
  • 13,620
  • 2
  • 29
  • 55
  • I'm still new to Python so can you please tell me how I can incorporate these into my code?Or explain what exactly your code is doing? – user3613025 Nov 13 '17 at 19:29
  • If you have a specific question I can try to answer it. As a beginner, I suggest looking up the documentation and working out what you can yourself first (for [`assert`](https://docs.python.org/3/reference/simple_stmts.html#assert) and [`linspace`](https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.linspace.html) for example). – Daniel F Nov 14 '17 at 06:31