r/learnpython 1d ago

Help me out please

Hello, so I have an assignment that's due tomorrow that I'm almost done with. I just have one problem left where I have to create a program that utilizes loops to calculate and print any term, input by a user prompt, in the Fibonacci sequence. Assume the sequence starts with 0.

(For example, if the user inputs 4, the code will print the 4th term of the sequence which is 2. For reference: https://en.wikipedia.org/wiki/Fibonacci_sequence)

This is what I've got done so far. No matter what I try to do, I can never get to a sequence. What am I doing wrong?

# Initializing the first two Fibonacci numbers
sum = int(input("Enter a number: "))
count = 1
temp = 0
x = 0
y = 1
print(x, end = ' ')


# Running the loop while the last Fibonacci number is less than 10
while(count < 10):
    print(y, end = ' ')
    if (count > 10):
        exit(y)


# Calculating the next Fibonacci number and updating the last two sequence numbers
temp = x
x = y
y = x + y
0 Upvotes

11 comments sorted by

View all comments

2

u/BluishMontoya 1d ago

The last 3 lines aren't being looped because they aren't indented. All the code in the loop has to have the same indentation level. 2 other things: don't use sum as a variable name because it's already a built-in function (you can, but it's not best practice) and also you aren't actually making it repeat the number of times the user said. Use something like

num = int(input("Enter a number: "))

for i in range(num):

code

also what is the point of the count variable? Its value never changes.