繁体   English   中英

Python 一行用于循环输入

[英]Python one line for loop input

我不知道如何根据用户选择获取输入。 即“您要输入多少个数字?” 如果回答 5,那么我的数组有 5 个空格,一行中有 5 个整数,由空格分隔。

num = []
x = int(input())
for i in range(1, x+1):
    num.append(input())

上层代码有效,但输入按回车(下一行)拆分。 IE:

2
145
1278

我想得到:

2
145 1278

我会很感激一些帮助。

编辑:

x = int(input())
while True:
    attempt = input()
    try:
        num = [int(val) for val in attempt.split(" ")]
        if len(num)== x:
            break
        else:
            print('Error')
    except:
        print('Error')

这似乎有效。 但是为什么我会收到“超出内存限制”的错误消息?

编辑:无论我使用哪种方法,我都会遇到同样的问题。

x = int(input())
y = input()
numbers_list = y.split(" ")[:x]
array = list(map(int, numbers_list))
print(max(array)-min(array)-x+1)

或者

x = int(input())
while True:
    attempt = input()
    try:
        num = [int(val) for val in attempt.split(" ")]
        if len(num)== x:
            break
        else:
            print('Error')
    except:
        print('Error')

array = list(map(int, num))
print(max(array)-min(array)-x+1)

或者

z = int(input())
array = list(map(int, input().split()))
print(max(array)-min(array)-z+1)

最简单的方法是这样的:

input_list = []

x = int(input("How many numbers do you want to store? "))

for inputs in range(x):
    input_number = inputs + 1
    input_list.append(int(input(f"Please enter number {input_number}: ")))

print(f"Your numbers are {input_list}")

您是否有理由希望输入仅在一行上? 因为您只能限制以这种方式存储(或打印)的数字数量,而不能限制用户输入的数量。 用户可以继续输入。

假设您想在一行中输入数字,这是一个可能的解决方案。 用户必须像您在示例中所做的那样拆分数字。 如果输入格式错误(例如“21 asd 1234”)或数字与给定长度不匹配,则用户必须再次输入值,直到进行有效输入。

x = int(input("How many numbers you want to enter?"))
while True:
    attempt = input("Input the numbers seperated with one space")
    try:
        num = [int(val) for val in attempt.split(" ")]
        if len(num)==x:
            print(num)
            break
        else:
            print("You have to enter exactly %s numbers! Try again"%x)
    except:
        print("The given input does not match the format! Try again")

您可以在不需要x情况下使用它:

num = [int(x) for x in input().split()]

如果您希望在一行中输入数字,只有空格,您可以执行以下操作:

x = int(input("How many numbers do you want to store? "))
y = input(f"Please enter numbers seperated by a space: ")
numbers_list = y.split(" ")[:x]
print(f"We have a list of {len(numbers_list)} numbers: {numbers_list}")

即使有人输入的数量超过承诺的数量,它也会返回承诺的数量。

输出:

How many numbers do you want to store? 4
Please enter numbers seperated by a space: 1 4 6 7
We have a list of 4 numbers: ['1', '4', '6', '7']

尝试这个。

x = int(input())
num = [] # List declared here
while True:
    try:
        # attempt moved inside while
        # in case input is in next line, the next line
        # will be read in next iteration of loop.
        attempt = input() 

        # get values in current line
        temp = [int(val) for val in attempt.split(" ")]

        num = num + temp
        if len(num) == x:
            break
    except:
        print('Error2')

也许机器人使用换行符而不是空格传递整数,在这种情况下,while 循环将永远不会终止(假设您的机器人持续发送数据),因为每次,它只会重新写入 num。

注意 - 此代码适用于空格和换行符分隔的输入

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM