How to add to each value of a list, a number that depends on another random?

Asked

Viewed 110 times

0

I have a list of 21 terms and a value that goes from 0 to a random number that varies from zero to the number typed by the user. I need to divide this random number into smaller numbers that add up to random, for example:

The random number generated was 100, I can divide into the numbers 50, 25, 10, 15 (and 17 more zeros) and then add each piece of this into a term I choose from the list (it can’t be random nor in order, it has to be arbitrary)

The problem is I don’t know how to do this, I thought I’d make a for that contains a random number q varies from 0 to the number typed, but not even assemble that I know, any idea?

(The scheme would be in the form:

x = randint (0,pat) #pat é a variável digitada pelo usuário
x2 = randint (0,pat - x)
x3 = randint (0,pat - x2)

Up until (pat minus the variable give zero), where the zero number would simply be repeated several times)

  • Buddy, you’re still confused what you want, that’s the whole code ?

  • The beginning of the code consists of three lists, with different sizes. The user must enter a number that will be divided into 3 random percentages. This means that I will have three numbers, each associated with one of the lists (One of these numbers is the pat variable). With pat I need to divide it again, in the number of times of the list associated with it and add to each variable this value, which can be equal to zero. @Fourzerofive

  • This issue is very confused. It is not clear whether it is the inputs nor the outputs. On this issue it was not clear, with which lists we should work, the type of variable of the list, nor the range of such lists. It was not clear what percentages would be. Very confusing this question.

1 answer

0

It is possible to cycle and subtract the inserted value in each iteration.

from random import randint

pat = int(input("Inserir número: ")) 

lista = []
valor_parcial = pat
for i in range(0,21):
    x = randint(0, valor_parcial)
    lista.append(x)
    valor_parcial -= x

print(lista)
  • I tried this, but pat would end up varying so it would be bigger than before, my solution was to use a while with 2 for and 3 ifs, but thanks for the help

Browser other questions tagged

You are not signed in. Login or sign up in order to post.