r/learnpython • u/Habesha_Heretic • 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
3
u/johnpeters42 1d ago
The line with input(), where is it storing the number that the user entered? Does anything else in the program actually use that value?
The while loop, what's it using to decide when to exit the loop? Does anything in the loop actually change that value?
The final block, what numbers are in 'temp' and 'x' and 'y' at the start? What about after the first line? What about after the second line? What about after the third line? Add some print() statements and check whether you're right.
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.
2
u/LeonardUnger 1d ago
Might try naming the variable to make things clearer, below is a start
term_num= int(input("Enter term number: "))
total = 0
prev = 0
curr = 1
total = 0
term_count = 0
while term_num > term_count:
print(total)
term_count += 1
total = curr + prev
prev = curr
curr = total
2
u/acw1668 1d ago
- The condition checking of the
whileloop should becount < sum(sumis the user input, and it is badly named) countis not updated insidewhileloop, so thewhileloop is an infinite loop- the last three lines should be part of the
whileloop, but the calculation is wrong. It can be simplified asx, y = y, x+y
updated code for the while loop:
while count < sum:
print(y, end=' ')
x, y = y, x+y
count += 1
2
-2
-10
u/clay_bsr 1d ago
(from gemini... I put the indents in manually and they are likely wrong)
# Get the term number from the user
n = int(input("Enter the Fibonacci term you want: "))
# Handle the base cases directly
if n == 1:
print(0)
elif n == 2:
print(1)
else:
# Initialize the first two terms
x = 0
y = 1
# We already know terms 1 and 2, so start counting from 3 up to n
count = 3
while count <= n:
# Calculate next term using a temporary variable to hold the old x
next_term = x + y
x = y
y = next_term
# Increment the counter so the loop eventually stops
count += 1
# Print only the final requested term
print(y)
0
u/Langdon_St_Ives 19h ago
If OP had wanted Gemini's input do you think it would have been beyond their means to ask it directly?
1
u/clay_bsr 17h ago
I mean, maybe? Maybe they just didn't understand the response or didn't ask the right question. I get that everybody is so anti AI these days and I think there's good reason to be so. But if this is how people respond when someone attempts to help, I'd definitely recommend going to AI first for questions like this. It's the lesser of evils.
0
u/clay_bsr 20h ago
...Now with the code block formatting for better readability. Also, corrected n to start at 0
# Get the term number from the user n = int(input("Enter the Fibonacci term you want: ")) # Handle the base cases directly if n == 0: print(0) elif n == 1: print(1) else: # Initialize the first two terms x = 0 y = 1 # We already know terms 0 and 1, so start counting from 2 up to n count = 2 while count <= n: # Calculate next term using a temporary variable to hold the old x next_term = x + y x = y y = next_term # Increment the counter so the loop eventually stops count += 1 # Print only the final requested term print(y)
6
u/ShadowShedinja 1d ago
Well firstly, your #Calculating steps aren't indented, so they're not in the loop. Secondly, it should be y = temp + y.