367,888 views
36 votes
36 votes
Write the Python code that will create a list with 50 elements. Populate (fill) the list using a

loop that will get an integer value from the user for each element in the list.

User Maninderpreet Singh
by
3.0k points

1 Answer

16 votes
16 votes

Answer:

"

integers = []

for _ in range(50):
userInput = int(input("Input an integer: "))

integers.append(userInput)

"

Step-by-step explanation:

So there is a very useful object or class in python that is called range, and this is primarily used to traverse certain values using a for loop. The syntax is defined as:

"range(start, stop, increment)"

where start is included, stop is excluded, and increment is how much the value increases after each iteration, although it is an optional value, just like stop"

So if you simply do:

"range(a)"

the start value will default to 0

the end value will be equal to "a"

the increment will default to 1

If you provide the following values:

"range(a, b)"

start will be equal to "a"

end value will be equal to "b"

increment will default to 1

If you provide all three values as such:

range(a, b, c)

start, end, and increment will be assigned to the variable values in their respective order.

You don't need to know all these 3 cases for this example, but it's best ton know in case you need to do something similar but slightly different in the future.

So, since we want to fill a list with 50 elements, and we want user input, we can loop a code 50 times and ask them to input an integer value and then append it to the list.

To run a piece of code 50 times, we can simply do:

"

for _ in range(50)

"

the "_" variable will be assigned to numeric values from 0-49 (50 is excluded), so it will run 50 times. I named it "_", because we really don't need these numeric values, we just need the for loop so we can have the code run 50 times.

So the next thing to do is initialize an empty list which we will append values to. This can simply be done by writing:

"

integers = []

"

and the variable name can really be anything (besides keywords), but I think it's the most appropriate given the context

So in the for loop, we want to ask for input using the input function, which asks the user for input, and then returns the input they input. Using this, we want to convert it to an integer using int (this is technically not a function, it's a class, you may learn about this more later if you haven't but it's not necessary to know for now), which converts strings to integers.

So using this we get the following code:

"

integers = []

for _ in range(50):
userInput = int(input("Input an integer: "))

integers.append(userInput)

"

the variable isn't necessary and you could just directly do

"

integers.append(int(input("Input an integer: ")))

"

but it's really for somewhat better readability, but both are equally valid, since the second statement isn't really to complicated.

One thing to note, is if the user inputs a non-integer string, the code will raise an error.

User Elliot Winkler
by
2.9k points