问题描述
我创建了一行斐波那契数字。首先需要输入数字来指定斐波那契数列的大小,实际上是行的大小。该数字必须是> = 2的整数。
结果是打印出所有斐波那契数,直到该行的最后一个数,并且它们各自的索引都在该行中!之后,需要取出该行的一个切片,结果是打印出该切片中的所有数字及其各自的索引。
我成功地掌握了排除不在指定范围内的所有值的方法,但是我没有成功排除数字和其他非期望类型的输入,例如,我想排除输入变量的浮点类型和字符串类型输入变量。
我指定了输入变量的不期望类型是float和string!但是它报告我一个错误!如何克服这个问题,或者换句话说,如何指定排除浮动变量和字符串变量以不向我报告错误的要求?
代码:
while True:
n = int(input('Please enter the size of Fibonacci row - positive integer number(N>=2)!'))
if n < 2:
print('This is not valid number! Please enter valid number as specified above!')
continue
elif type(n)==float: # this line is not working!
print('The number has to be an integer type,not float!')
continue
elif type(n)==str: # this line is not working!
print( 'The number has to be an integer type,not string!')
continue
else:
break
def __init__(self,first,last):
self.first = first
self.last = last
def __iter__(self):
return self
def fibonacci_numbers(n):
fibonacci_series = [0,1]
for i in range(2,n):
next_element = fibonacci_series[i-1] + fibonacci_series[i-2]
fibonacci_series.append(next_element)
return fibonacci_series
while True:
S = int(input('Enter starting number of your slice within Fibonacci row (N>=2):'))
if S>n:
print(f'Starting number can not be greater than {n}!')
continue
elif S<2:
print('Starting number can not be less than 2!')
continue
elif type(S)==float: # this line is not working!
print('The number can not be float type! It has to be an integer!')
continue
elif type(S)==str: # this line is not working!
print('Starting number can not be string! It has to be positive integer number greater than or equal to 2!')
continue
else:
break
while True:
E = int(input(f'Enter ending number of your slice within Fibonacci row(E>=2) and (E>={S}):'))
if E<S:
print('Ending number can not be less than starting number!')
continue
elif E>n:
print(f'Ending number can not be greater than {n}')
continue
elif E<2:
print('Ending number can not be greater than 2!')
continue
elif type(E)==float: # this line is not working!
print('Ending number can not be float type! It has to be an integer type!')
continue
elif type(E) ==str: # this line is not working!
print(f'Ending number can not be string! It has to be positive integer number greater than or equal to {S}')
continue
else:
break
print('Fibonacci numbers by index are following:')
for i,item in enumerate(fibonacci_numbers(n),start = 0):
print(i,item)
fibonacci_numbers1 = list(fibonacci_numbers(n))
print('Fibonacci numbers that are within your slice with their respective indices are following:')
for i,item in enumerate(fibonacci_numbers1[S:E],start = S):
print(i,item)
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)