python 函数浅尝 (斐济函数)

代码
IndentationError: unexpected indent
>>> def fib(n):
...     
"""Print a Fibonacci series up to n"""
...     a , b 
= 0 , 1
...     
while a < n:
...             
print(a,end=' ')
...             a, b 
= b ,a+b
...     
print()
...
>>>
...     fib(
2000)
  File 
"<stdin>", line 2
    fib(
2000)
    
^
IndentationError: unexpected indent
>>>
...  fib(
2000)
  File 
"<stdin>", line 2
    fib(
2000)
    
^
IndentationError: unexpected indent
>>> fib(2000)
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597
python函数的调用方式与其它语言的方式没有多但的区别,倒是python 变量赋值有点特别
 
a b = 1, 2 要注意空格
 
 
 
  以下是用c实现的,以表示我对开发语言了解广泛。
 
代码
#include <stdio.h>


int main (int argc, const char * argv[]) {
     
     
int numToPrint, i;
     
double num1, num2, ans;
     
     num1 
= 0;
     num2 
= 1;
     
     printf(
"How many numbers would you like to print?\t");
     scanf(
"%d"&numToPrint);
     
     printf(
"1\t%.0f\n2\t%.0lf\n", num1, num2);
     
     
for(i = 0; i < (numToPrint - 2); ++i){
          
          ans 
= (num1 + num2);
          
          printf(
"%d\t%.0lf\n", i+3, ans);
          
          num1 
= num2;
          num2 
= ans;
     }
     
     
    
return 0;
}
原文地址:https://www.cnblogs.com/chenli0513/p/1869237.html