ex de python help!

Asked

Viewed 52 times

-1

I caught it in an exercise. in the code you need to have a number n between 0 and 100 and in the next lines a variable representing how much money each one has and be printed the minimum amount for each one to have 2000. Input ex: 2,999, 1050; output: 1001, 950. 2, 0, 2000; output: 2000. 1, 900; output: 1100. I can make him return the exit but only one. like: I can’t figure out the mistake, if you can help me...

n = 0
q = 0
d = 0
while 0 <= n <= 100:
    n = int(input())
    while d<2000:
        d = int(input())
        q = 2000-d
        break
print(f'{q}')
  • And what is the meaning of this number n that you read right away? Note that you read and forget, do not use for anything.

  • 1

    the n is the number of people, if the n is equal to two o d should receive two values, but I could not, I could only make him return 1

  • Hello, @Luiza. The problem is only logical: 1 - you want to read the values of d before you do the calculation; 2 - write the answer... I’ll fix it here.

  • 3

    This post is relevant to problems of this type: What is the Table Test? How to apply

1 answer

0


I made some changes to tidy up the logic. There are many ways to do it, I opted for the simplest.

arr_valores = []
arr_falta = []

# essa varíavel só interfere no if
# para impedir a leitura que mais de 100 valores
n = int(input("Numero de pessoas: "))  # lendo o número de pessoas
if 0 <= n <= 100:
    # lendo os valores
    for _ in range(n):
        arr_valores.append(int(input("valor: ")))
    
    # fazendo o processamento
    for i in range(n):
        arr_falta.append(2000 - arr_valores[i])
    
    # escrevendo o resultado
    for i in range(n):
        print(f'pessoa {i} falta {arr_falta[i]} para 2000')

  • thank you so much!!

Browser other questions tagged

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