Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 打印具有一定长度的第一个斐波那契数_Python_Python 3.x_While Loop_User Input_Fibonacci - Fatal编程技术网

Python 打印具有一定长度的第一个斐波那契数

Python 打印具有一定长度的第一个斐波那契数,python,python-3.x,while-loop,user-input,fibonacci,Python,Python 3.x,While Loop,User Input,Fibonacci,所以我基本上只是想弄清楚如何让这个程序打印出第一个Fibonacci数,长度与用户输入的数字相同。斐波那契计数部分很好,我只是在努力理解如何打印出正确对应的数字。在理解这一行代码的具体原因方面存在问题: print(str(result[user])) 不起作用,我需要做什么才能使它起作用 def fibonacci(): previous_num, result = 0, 1 user = input('please enter the length of the fibon

所以我基本上只是想弄清楚如何让这个程序打印出第一个Fibonacci数,长度与用户输入的数字相同。斐波那契计数部分很好,我只是在努力理解如何打印出正确对应的数字。在理解这一行代码的具体原因方面存在问题:

print(str(result[user]))
不起作用,我需要做什么才能使它起作用

def fibonacci():
    previous_num, result = 0, 1
    user = input('please enter the length of the fibonacci number you would like to see')
    while True:
        previous_num, result = result, previous_num + result
        print(result)
        print(str(result[user]))


fibonacci()

让我们检查一下你被卡住的线路:

print(str(result[user]))
代码在这里试图做的是访问变量
result
user
属性,将其转换为
str
,然后打印它。它不起作用,因为变量
result
是一个整数,没有任何可以使用方括号访问的内容

您可能需要打印
结果
变量的长度。这可以通过使用
str
result
转换为字符串,并使用
len
查找其长度来实现

print(len(str(result)))
这与上一行相结合,
print(result)
,将打印出斐波那契数以及数字的长度(以位数表示)

例如:

1
1
2
1
3
1
5
1
8
1
13
2
...
def fibonacci():
    previous_num, result = 0, 1
    user = int(input('please enter the length of the fibonacci number you would like to see: '))
    while len(str(result)) < user:
        previous_num, result = result, previous_num + result
    return result

print(fibonacci())
please enter the length of the fibonacci number you would like to see: 10
1134903170
这不是我们想要的-我们想要我们的函数返回第一个Fibonacci数,它包含用户输入的位数。为此,我们需要将
while
条件编辑为以下内容:

while len(str(result)) < user:
输出:

1
1
2
1
3
1
5
1
8
1
13
2
...
def fibonacci():
    previous_num, result = 0, 1
    user = int(input('please enter the length of the fibonacci number you would like to see: '))
    while len(str(result)) < user:
        previous_num, result = result, previous_num + result
    return result

print(fibonacci())
please enter the length of the fibonacci number you would like to see: 10
1134903170
或者:@Sadap在评论中

def fibonacci():
    previous_num, result = 0, 1
    user = int(input('please enter the length of the fibonacci number you would like to see: '))
    min_num = 10**(user-1)
    while result < min_num:
        previous_num, result = result, previous_num + result
    return result

print(fibonacci())
def fibonacci():
上一个数值,结果=0,1
user=int(输入('请输入希望看到的斐波那契数的长度:'))
最小数量=10**(用户1)
当结果<最小值时:
上一个数值,结果=结果,上一个数值+结果
返回结果
打印(斐波那契())

回答很好,但是输入后,写
min\u num=10**(user-1)
然后while条件是
result
该解决方案比将每个可能的结果转换为str以获得长度要快得多