[英]How to set only n number of inputs for input().split() in python?
Problem : 1.The first line contains an integer n, denoting the number of elements in the tuple.问题: 1.第一行包含一个整数n,表示元组中元素的数量。 2.The second line contains n space-separated integers describing the elements in tuple.
2.第二行包含 n 个以空格分隔的整数,描述元组中的元素。
Sample Input(correct):样本输入(正确):
2
3 6
sample input(wrong):样本输入(错误):
2
3 6 8
Example:例子:
n=int(input()) #denotes number of elements should be in a tuple
integer_list = tuple(map(int, input().split())) # how to modify this line to take only n space separated inputs
#suppose if n=2 in first line then
#In second line I need only 2 space-separated inputs(since n=2) i.e 3 9(or any two),
but not more than or less than n numbers.
**For Example**:5 7 3(which is >n) or 5(which is <n))
The following code works, but I do not think it's possible to create the tuple in one line:以下代码有效,但我认为不可能在一行中创建元组:
def main():
n = 0
while True:
try:
n = int(input())
except ValueError:
print("Wrong type entered (must enter integer number). Enter again: ")
continue
else:
break
if n == 0:
return -1
integer_list = []
string_input = input()
count = 0
for i in string_input.split():
if count == n:
break
x = 0
try:
x = int(i)
except ValueError:
return -1
else:
integer_list.append(x)
count += 1
if count != n:
return -1
integer_tuple = tuple(integer_list)
print(f"The tuple: {integer_tuple}")
return 0
if __name__ == "__main__":
main()
It will ask the user again and again for an integer if they have not entered one (as the first input), and then only creates a tuple if n subsequent space-separated integer numbers are entered (not less than n or more than n, and not if there is an invalid input).如果用户没有输入一个整数(作为第一个输入),它将一次又一次地询问用户一个整数,然后仅在输入 n 个后续空格分隔的整数(不小于 n 或大于 n,如果输入无效,则不会)。
Hope this helps.希望这可以帮助。
Instead of input()
, define your own function that checks for the condition, and raise exception if condition failed.代替
input()
,定义您自己的检查条件的函数,如果条件失败则引发异常。
Example:例子:
def get_input(n):
string_splits=input().split(' ')
if len(string_splits)!=n:
raise Exception("Invalid input")
return string_splits
n=int(input())
integer_list = tuple(map(int, get_input(n))
If you want the output only takes n input and ignore the rest of it wheter it less or more than n value, you could try this:如果您希望输出只接受 n 个输入并忽略它的其余部分,无论它小于还是大于 n 值,您可以试试这个:
n=int(input())
integer_list = tuple(map(int, input().split()))
if len(integer_list) != n: integer_list = tuple(integer_list[0:n]) # add this
print(integer_list)
or using function and exception handling:或使用函数和异常处理:
def input_tuple(i, n):
i = i.split()
i = [int(num) for num in i if num.isnumeric()] # ignores non-digit
return tuple(i[0:n])
while True:
try:
n = int(input())
except ValueError:
print("Numbers only!")
continue
else:
break
integer_list = print(input_tuple(input(), n))
Example #1:示例 #1:
2 3 9 (3, 9)
Example #2:示例 #2:
2 5 7 3 (5, 7)
Example #3:示例#3:
1 5 7 3 (5,)
Example #4:示例 #4:
4 5 7 3 (5, 7, 3)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.