如何在 Python 中排除特定类型的输入变量?
How to exclude specific type of an input variable in Python?
我创建了一行斐波那契数列。一开始需要输入数字来指定斐波那契数列的大小,实际上是行的大小。数字要求为整数>=2.
结果是打印出所有斐波那契数,直到行的最后一个数字,以及行内各自的索引!之后需要取出行的一部分,结果是打印出切片内的所有数字及其各自的索引。
我成功掌握了排除所有不在指定范围内的值,但是我没有成功排除数字和其他不需要类型的输入,示例想排除输入变量的 float 类型和字符串输入变量的类型。
我指定输入变量的不良类型是浮点型和字符串型!但是它报告我一个错误!如何克服这个问题,或者换句话说,如何指定排除浮动变量和字符串变量的要求而不向我报告错误?
代码:
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)
第一行
n = int(input('Please enter the size of Fibonacci row - positive integer number(N>=2)!'))
您正在将输入转换为 int,因此无论用户提供什么,它都将被转换为 int。
如果您希望您的代码正常工作,请将其替换为此
n = input('Please enter the size of Fibonacci row - positive integer number(N>=2)!')
使用 try/except/else
测试输入。 int()
如果字符串值不是严格的整数,则引发 ValueError
。
>>> while True:
... s = input('Enter an integer: ')
... try:
... n = int(s)
... except ValueError:
... print('invalid')
... else:
... break
...
Enter an integer: 12a
invalid
Enter an integer: 1.
invalid
Enter an integer: 1.5
invalid
Enter an integer: 1+2j
invalid
Enter an integer: 5
>>>
如果您需要检查类型,isinstance
通常更好,例如。 g.:
if isinstance(var, int) or isinstance(var, str):
pass # do something here
已解决 :-) 只需在您的代码中添加 try except 块,如下所示:
while True:
try:
num = int(input("Enter an integer number: "))
break
except ValueError:
print("Invalid input. Please input integer only")
continue
print("num:", num)
点赞和检查:-)
我创建了一行斐波那契数列。一开始需要输入数字来指定斐波那契数列的大小,实际上是行的大小。数字要求为整数>=2.
结果是打印出所有斐波那契数,直到行的最后一个数字,以及行内各自的索引!之后需要取出行的一部分,结果是打印出切片内的所有数字及其各自的索引。
我成功掌握了排除所有不在指定范围内的值,但是我没有成功排除数字和其他不需要类型的输入,示例想排除输入变量的 float 类型和字符串输入变量的类型。
我指定输入变量的不良类型是浮点型和字符串型!但是它报告我一个错误!如何克服这个问题,或者换句话说,如何指定排除浮动变量和字符串变量的要求而不向我报告错误?
代码:
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)
第一行
n = int(input('Please enter the size of Fibonacci row - positive integer number(N>=2)!'))
您正在将输入转换为 int,因此无论用户提供什么,它都将被转换为 int。
如果您希望您的代码正常工作,请将其替换为此
n = input('Please enter the size of Fibonacci row - positive integer number(N>=2)!')
使用 try/except/else
测试输入。 int()
如果字符串值不是严格的整数,则引发 ValueError
。
>>> while True:
... s = input('Enter an integer: ')
... try:
... n = int(s)
... except ValueError:
... print('invalid')
... else:
... break
...
Enter an integer: 12a
invalid
Enter an integer: 1.
invalid
Enter an integer: 1.5
invalid
Enter an integer: 1+2j
invalid
Enter an integer: 5
>>>
如果您需要检查类型,isinstance
通常更好,例如。 g.:
if isinstance(var, int) or isinstance(var, str):
pass # do something here
已解决 :-) 只需在您的代码中添加 try except 块,如下所示:
while True:
try:
num = int(input("Enter an integer number: "))
break
except ValueError:
print("Invalid input. Please input integer only")
continue
print("num:", num)
点赞和检查:-)