主要功能不是运行?使困惑
Main function not running? CONFUSED
我正在尝试为 class 编写一个模块,但我的 main() 函数不起作用停止执行,只允许我输入更多数字,然后转到下一行 - 无穷无尽。
我进入了python的主要功能,但我还是一头雾水。
# 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()
我希望代码 return 输入的第 n 个斐波那契数的最后一位。
而不是 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))
我正在尝试为 class 编写一个模块,但我的 main() 函数不起作用停止执行,只允许我输入更多数字,然后转到下一行 - 无穷无尽。
我进入了python的主要功能,但我还是一头雾水。
# 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()
我希望代码 return 输入的第 n 个斐波那契数的最后一位。
而不是 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))