作业帮 > 英语 > 作业

python练习题This question is about Fibonacci number.For your in

来源:学生作业帮 编辑:作业帮 分类:英语作业 时间:2024/05/12 07:04:05
python练习题
This question is about Fibonacci number.For your information,the Fibonacci sequence is as follows:
0,1,1,2,3,5,8,13,21,34,55,89,144,233,...
\x05\x05\x05\x05\x05
That is,the first two Fibonacci numbers are 0 and 1,each Fibonacci number after that is equal to the sum of the
two numbers that precede it.For example,the third Fibonacci number is equal to the sum of the first and
second number,the fourth number is equal to the sum of the second and third number,and so on ...
\x05\x05\x05\x05\x05
Write a program that asks the user to enter a positive integer n,then your program should print/output in one
line the Fibonacci sequence up to n.
For example,if n is 100,your program should output 0,1,1,2,3,5,8,13,21,34,55,89,
If n is 8,your program should output 0,1,1,2,3,5,8,
不好意思因为才学只能用 条件命令和loop
up_limit = int(input("please enter a positive integer:"))
print(" the Fibonacci sequence up to %d:" %(up_limit))
t1,t2=0,1
a=[t1,t2]
while 1:
    t1,t2=t2,t1+t2
    if t2<=up_limit:
        a.append(t2)
    else:
        break
print(','.join(str(i) for i in a))
再问: 不好意思,我们还没学函数只能用条件和loop
再答: up_limit = int(input("please enter a positive integer:"))
t1,t2=0,1
a=[t1,t2]
while 1:
    t1,t2=t2,t1+t2
    if t2<=up_limit:
        a.append(t2)
    else:
        break
#print(' '.join(str(i) for i in a))
for i in a:
    print('%d'%i,end=',') 再说,我上面也没用函数把