Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need to Convert
#1
I need to convert a code for class from an "If/Else, For Loop" into a "While" statement.

This is the code we started with:
n = int(input())
if n == 0:
print(0)
else:
a, b = 0, 1
for i in range(2, n + 1):
a, b = b, a + b
print(b)
This is what I have so far:
n = int(input())
a, b = 0, 1
while a == b:
a, b = b, a + b
print(b)
My code is completely wrong but I'm not sure what to do...
Reply
#2
The good old Fibonacci sequence !

Well, the two programs you posted are not properly indented, I hope you understand the importance of this in python! The for loop in the initial program is:
for i in range(2, n + 1):
How many times is it executed? Does it depend on the values of a or b? How would you translate it into a while loop?
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020