-3

How to make the code calculate not one number, but several? That is, so that the user can enter several times a different number of the number from which we subtract? and so that later it would be possible to build a graph from the obtained numbers.

f1 = 1
f2 = 1

n = int(input("Enter the number from which we subtract the previous one:"))

for i in range(n - 1):
    f2,f1 = f2+f1, f2
print(f2**2 - f1**2)
  • Does this answer your question? [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – Ignatius Reilly Oct 15 '22 at 17:58

1 Answers1

1

I'm not sure I fully understand what you are trying to do but why not input a list of input numbers to a function which does the processing and return a list of results which is then usable for graph plotting.

import matplotlib.pyplot as plt

numbers = input("enter a series of numbers separated by a space ")
listnumbers = [int(i) for i in numbers.split(" ") if i.isdigit()]


def f(mynums):
    result = []
    for n in mynums:
        f1 = 1
        f2 = 1
        for i in range(n - 1):
            f2, f1 = f2 + f1, f2
        result.append(f2**2 - f1**2)
    return result


x = f(listnumbers)

plt.plot(listnumbers, x)
plt.show()
user19077881
  • 3,643
  • 2
  • 3
  • 14
  • yes, you understood me correctly. only I want the user to enter the "x" himself – Alexey Byrnov Oct 15 '22 at 17:22
  • I've edited the above code so the user types in the list. Is that what you intend? – user19077881 Oct 15 '22 at 18:35
  • not quite. as a result, the code outputs only 1 number, and I need to have several of them. that is, to do this cycle several times so that the user enters a number several times and the code outputs several numbers – Alexey Byrnov Oct 16 '22 at 03:00
  • I don't understand. The above code asks for a series of numbers and outputs a List of several numbers. For example: in response to the prompt the user enters 3 5 8 then the result is [5, 39, 715]. Because the input and output are both in Lists this makes it easy to plot in matplotlib or whatever. If instead you wanted a loop asking the user to input another number than that is easy to do but you would need a Quit 'signal' and the numbers would need to be converted to a List anyway. Please explain what you want. – user19077881 Oct 16 '22 at 07:07
  • Ah, I didn't see that the code had changed. Sorry, that's right. Thank you very much – Alexey Byrnov Oct 17 '22 at 10:38
  • is it possible to build a graph from these points somehow? for example through numpy and matplotlib.pyplot – Alexey Byrnov Oct 17 '22 at 10:44
  • Yes, it's very easy. Do you want to plot the input list against the output list? What sort of a graph (scatter, line, bar ... etc) – user19077881 Oct 17 '22 at 10:46
  • well, for example, just a line on a graph with X and Y coordinates. – Alexey Byrnov Oct 17 '22 at 11:50
  • OK. I edited the code to show a basic plot. You can explore how to change type of plot, add axis labels or whatever you want. Please now accept the answer, – user19077881 Oct 17 '22 at 12:09
  • thanks. and how to do it? :) – Alexey Byrnov Oct 17 '22 at 13:18
  • click on the grey tick symbol to accept an answer and it changes to green, – user19077881 Oct 17 '22 at 13:25