array = [] import random for i in range(10): i = i + 1 random_number = random.randint(1,100) array.append(random_number) while i == 10: break if i == 10: print(array) 

Please help simplify the code. After filling the array into 10 values, print this array. Preferably without using the if statement.

Python 3.5 is used

3 answers 3

To summarize everything that has been said and not said: (considering that you seem to be a beginner, let's start in order):

  1. As @insolor noted: it is not necessary to increase the variable i by one each time; it will increase anyway.
  2. There is no need for while , the for loop itself should be completed after 10 iterations (“repetitions” if it is easier to express it). And there is also no point in the if construct - it is quite logical that after the completion of the cycle, the variable i will be equal to 10: there is simply no options.

Those. The code can be simplified to this:

 array = [] import random for i in range(10): random_number = random.randint(1,100) array.append(random_number) print(array) 

Further, there is such a thing as “List Generators”, if you don’t know that you should read about it, it’s not difficult.

A simple example of list generators:

 array = [1, 2, 4, 5, 6, 98, 123, 45] # % — операция вычисления остатка от деления # если остаток от деления на 2 равен единице, то число нечетно only_odd_numbers = [n for n in array if n % 2 == 1] print(only_odd_numbers ) # [1, 5, 123, 45] 

Using the list generators, you can further simplify the code:

 import random array = [random.randint(1,100) for i in range(10)] print(array) 

UPDATE:

There is also a function such as random.choices . The essence of this function is by examples:

 import random arr1 = random.choices([1, 2], k=10) arr2 = random.choices(range(1, 10)) arr3 = random.choices(range(1, 100), k=10) arr4 = random.choices(range(1, 10), k=20) print(arr1) # [1, 1, 1, 2, 2, 1, 2, 1, 2, 2] print(arr2) # [8] print(arr3) # [60, 35, 80, 26, 7, 65, 58, 9, 77, 2] print(arr4) # [1, 1, 6, 4, 2, 6, 7, 2, 2, 2, 2, 1, 1, 6, 1, 7, 8, 3, 5, 3] 

Note: arr1 contains only one and two.

Thus: random.choices returns a list consisting of a certain number (determined by the parameter k ) of elements, each of which is a random element of the sequence that we passed in the first argument.

UPDATE An important remark was made by @jfs: “ randint includes the right border, and range does not”

  • one
    note randint includes the right border, but range does not. random.choices(range(1,101),k=10) - jfs

In any case, your list is filled with 10 values, so if not needed. Just print list using print after the loop.

What else is needed:

  • i = i + 1 because i and since in your cycle i and so on each iteration it automatically becomes 1 more.
  • while with a break inside a for loop does not make sense in this case. The for loop finishes execution after 10 iterations. Just remove while .

Total remains:

 import random array = [] for i in range(10): random_number = random.randint(1,100) array.append(random_number) print(array) 

    Do not reinvent the wheel, in Python all inclusive:

     import random random.sample(range(1, 100), 10) 

    range(from, to) returns an iterator to the set of integers from from to to with a step of 1, and sample(population, k) will select k random elements from the set of population .

    https://docs.python.org/3/library/random.html#random.sample

    • If a person does not yet understand how the for loop and other basic language constructs work, then it’s somewhat too early for iterators to learn. - insolor
    • one
      There is one problem in your solution, in this case, of course, everything is fine, but what if you need an array of 1000 random numbers between 0 and 9? random.sample returns only a list of unique elements. - Ruberoid
    • You are right, of course. But, as they say, "without normal TZ, the result will be HZ." The statement of work was "simplify the code, preferably without if," the result fully satisfies it. There were no requirements for the quality of the generated numbers. But at the time of this writing, the TC has as many as 3 (not counting its own) options for solving its problem. - fori1ton
    • In principle, yes, you are right. But expect a normal TK from a person just starting to learn Python, you see, it is hardly worth it. - Ruberoid