-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)
1

1 Answer 1

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()
Sign up to request clarification or add additional context in comments.

11 Comments

yes, you understood me correctly. only I want the user to enter the "x" himself
I've edited the above code so the user types in the list. Is that what you intend?
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
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.
Ah, I didn't see that the code had changed. Sorry, that's right. Thank you very much
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.