Fibonacci in Python - Simple solution [duplicate]

2024/11/8 17:43:41
n1 = 1
n2 = 1
n3 = n1 + n2
for i in range(10):n1 + n2print(n3)n1 = n2n2 = n3

According to what I know, this should be the simplest way of outputting the first 10 digits of the series, however, it prints 2 10 times. I don't understand why n1 doesn't get set to n2, and n2 doesn't get set to n3 after n3 has been printed.

Answer

There are many issues with your code. And you should first learn and try as much as you can on your own. I am also a beginner so I know what you are thinking. For some quick edits to make it workable:

n1 = 0
n2 = 1
n3 = 0
for i in range(10):n3 = n1 + n3print(n3)n1 = n2n2 = n3
  1. The series starts with 0, you initialized it with 1.
  2. The update statement n3=n1+n2 is outside the loop, how will it update? What is happening here is n3 = 1 + 1 = 2 in your code stays the same and it doesn't change.
https://en.xdnf.cn/q/120587.html

Related Q&A

snake game: snake colliding with itself

Hi I am currently writing a snake game code and I am nearly finished however I am having difficulty writing a code which will cause the game to end if the head of the snake collides with its body, I th…

python3 function not defined even though it is

when I try to call the changeProfile function, I keep getting the error "getAuthCode is not defined" even though it is clearly defined. Why do I keep getting this error? What am I doing wron…

Python maximum and minimum

Im supposed to write a function max_and_min that accepts a tuple containing integer elements as an argument and returns the largest and smallest integer within the tuple. The return value should be a t…

Get differences between two excel files

Problem SummaryGiven 2 excel files, each with 200 columns approx, and have a common index column - ie each row in both files would have a name property say, what would be the best to generate an output…

Most pythonic way to call a list of functions [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 4…

Unable to get wanted output using for and range functions

For my homework assignment, I am using the for loop and the range function. I have to create a loop that printsHello 0 Hello 1 Hello 3 Hello 6 Hello 10The question says that the number corresponds to t…

i want to add a new field to an existing module odoo11 but i dont know why it didnt work

product_template.xmlthe view to add the customizing field to the product module<?xml version="1.0" encoding="utf-8"?><odoo><data><record id="product_temp…

How to check with more RegEx for one address in python using re.findall()

How to check with more RegEx for one address in python using re.findall()Ex: I want to apply the below regex rules # need to get addresstxt = "hello user 44 West 22nd Street, New York, NY 12345 fr…

Finding all roots of an equation in Python

I have a function that I want to find its roots. I could write a program to figure out its roots but the point is, each time that I want to find the other root I should give it an initial value manuall…

defining matrix class in python

Define a class that abstracts the matrix that satisfies the following examples of practice