-2

I am new to coding and I need to plot an array but I could not modify my code to ask the user to enter the array.

passengersByQueue = [5,4,6,1,7,7]

I need to make the user to input 6 numbers instead of the numbers in the code

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
rex3th
  • 1

2 Answers2

2

You could use input() with a loop:

arr = []
count = 6
while count > 0:
  arr.append(input('Enter a number: '))
  count = count - 1

or use split():

userInput = input('Enter a comma-separated list of numbers')
arr = userInput.split(',')

Edit to add parsing of integers:

The returned array is an array of strings, so you need to convert them back to integers:

newArr = list(map(int, arr))

map(int, arr) returns an iterator of the array after the function int() is applied to all members of the array, you have to use list() in order for it to return an actual array. Alternatively, if you use the loop to have the user enter each value one by one, you can use int() before appending to the array.

Cris
  • 209
  • 2
  • 10
1

If you are looking to use pythons inbuilt function input(), then you can set a loop like this to read your input list

    li = []
    for i in range(n):
      x = input() #get input 
      li.append(int(x)) # append to list

   
Ajay Rathore
  • 124
  • 1
  • 7