0

I don't know how to get input depending on user choice. I.e. 'How many numbers you want to enter?' if answers 5, then my array has 5 spaces for 5 integers in one line separated by space.

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

Upper code works, however inputs are split by enter (next line). I.e.:

2
145
1278

I want to get:

2
145 1278

I would be grateful for some help.

EDIT:

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')

This seems to work. But why I'm getting "Memory limit exceeded" error?

EDIT: Whichever method I use, I get the same problem.

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

or

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)

or

z = int(input())
array = list(map(int, input().split()))
print(max(array)-min(array)-z+1)
9
  • You can not prevent user to enter 5 number split by space. because after pressing enter the values receives in input() in your python code. you can only choose the first 5. is it ok ? Commented Mar 2, 2019 at 12:01
  • You cant limit the user entering more than the specified number, you might want to rethink on how you want to structure your input. Commented Mar 2, 2019 at 12:45
  • Ok, so explain to me how you escape out of the while loop. Is there ANY condition where it ends en continues to the end of the script? Commented Mar 2, 2019 at 13:49
  • I could not tell you why, as you did not provide the line of my code where this error occurs nor the input string you used. I can't see a reason why this code should exceed your memory limit, if the string you put in is small. If this happens with all code provided, the error is maybe in another part of your code. Commented Mar 2, 2019 at 13:50
  • It's all code provided. This exercise is going to be corrected by the bot. It is programmed to enter number of inputs in the first line, and in the next line it enters x integers separated by space. For array = list(map(int, input().split())) or num = [int(x) for x in input().split()] I'm getting "memory limit exceeded". Commented Mar 2, 2019 at 13:54

5 Answers 5

1

Assuming you want to input the numbers in one line, here is a possible solution. The user has to split the numbers just like you did in your example. If the input format is wrong (e.g. "21 asd 1234") or the number does not match the given length, the user has to enter the values again, until a valid input is made.

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")
Sign up to request clarification or add additional context in comments.

1 Comment

This solution quite works. Please check my edited post.
0

The easiest way to do this would be something like this:

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}")

Is there a reason you want the input to be on one line only? Because you can only limit how many numbers you store (or print) that way, not how many the user inputs. The user can just keep typing.

1 Comment

There is a specific reason. This exercise is going to be corrected by the bot. It is programmed to enter number of inputs in the first line, and in the next line it enters x integers separated by space. For array = list(map(int, input().split())) or num = [int(x) for x in input().split()] I'm getting "memory limit exceeded".
0

You can use this without needing x:

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

Comments

0

If you want the numbers to be entered on one line, with just spaces, you can do the following:

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}")

Even if someone enters more than the amount of promised numbers, it returns the promised amount.

Output:

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']

2 Comments

I like your solution, but I get error described in the edited post. =(
Then there's probably a problem on the bot you're entering it into. This should in no way use too much memory.
0

Try this.

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')

Maybe the bot was passing integers with newline instead of spaces, in which case the while loop will never terminate (assuming your bot continuously sends data) because every time, it will just re-write num.

Note - this code will work for both space as well as newline separated inputs

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.