'While condition not breaking
Why while condition does not work for this case ?
def Fibonacci():
user = int( input("Type the length of Fibonacci numbers will be generated: ") )
fbnc_list = [0, 1]
_continue_ = True
while _continue_:
for n in range(0, user + 1):
fbnc_list.append( fbnc_list[n] + fbnc_list[n + 1] )
#print(fbnc_list, len(fbnc_list), user)
if len(fbnc_list) == user: _continue_ = False
return (fbnc_list)
Fibonacci()
On the comment line, i see that when i input 10 it should break on this case
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34] 10 10
len(fbnc_list) == user / 10 == 10.
So _continue is set to False and while should stop.
Not what it's happening.
Solution 1:[1]
how about this?
def Fibonacci():
user = int( input("Type the length of Fibonacci numbers will be generated: ") )
fbnc_list = [0, 1]
_continue_ = True
while _continue_:
for n in range(0, user + 1):
fbnc_list.append( fbnc_list[n] + fbnc_list[n + 1] )
#print(fbnc_list, len(fbnc_list), user)
if len(fbnc_list) == user: _continue_ = False;break
return (fbnc_list)
Fibonacci()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Shawn Ramirez |
