我想编写一类的模块,但我的main()函数不工作我相信代码是正确的,但是当我输入一个数它只是到下一行,不停止执行,只是让我输入进一步数字,去到下一行之后 - 循环往复。
我踏入蟒蛇的主要功能,但我仍然感到困惑。
# Uses python3
import sys
def get_fibonacci_last_digit_naive(n):
if n <= 1:
return n
previous = 0
current = 1
for _ in range(n - 1):
previous, current = current, previous + current
return current % 10
def fast_fibL(b):
a = []
a.append(0)
a.append(1)
n = 0
if (b == 0):
return 0
if (b == 1):
return 1
for i in range(b):
c = a[i] + a[i + 1]
a.append(c)
n += 1
return(a[n])
def get_fib_last_digit_fast(e):
b = fast_fibL(e)
return b % 10
def main():
input = sys.stdin.read()
n = int(input)
print(get_fib_last_digit_fast(n))
if __name__ == '__main__':
main()
我希望代码返回进入第n个Fibonacci数的最后一位。
相反input = sys.stdin.read()
的,使用built-in input()
function:
def main():
n = int(input('Enter an integer: '))
print(get_fib_last_digit_fast(n))
该计划正在等待你的输入,因为你使用stdin.read()
。此等待,直到输入由(例如)按下Ctrl-d终止。通常你会使用input()
这一点,其内容从标准输入一行。
def main():
line = input('> ')
n = int(line)
print(get_fib_last_digit_fast(n))