I have a job in Python 3 and do not know how to proceed

Asked

Viewed 122 times

-3

I need to make an algorithm that asks the user for numbers and stores them in a 20 position vector. Create a function that receives the filled vector and replace all occurrences of negative values with 0, those of values less than 10 by 1 and the others with 2. I searched a lot but could not do the exercise

What I’ve done so far:

  numeros = [int(input("Número: ")) for i in range(20)]

  numeros = [0 for item in numeros if item <= 0]
  numeros = [1 for item in numeros if item < 10]
  numeros = [2 for item in numeros if item >= 10]

  print(
      numeros
  )

The problem and my doubt is that the value exchange part of the vector is not working, I wanted to know where I went wrong.

  • 1

    Separate problems into two questions and detail as much as you can. You could explain exactly what you did in your code?

  • Leonardo, you need to try to make the problem and when a doubt arises you search here on the site, because it is almost certain that someone already had your doubt. If you still don’t find an answer, then you open a question with your question. What part of the problem do you not understand?

  • I did an input for the user to enter the values, and after that I tried to get all numbers smaller or equal to 0 of the vector to be exchanged for 0, and the same thing for the ones below. But when running after entering the numbers the process is finished and the value substitution part does not work.

  • 1

    You are reassigning the variable numeros every time; first reading the 20 numbers; second filtering the list of less than or equal to zero, replacing the value with 0 (i.e., it will be a list of zeroes only); third is replacing all values less than 10 by 1, but since the list is zeros integer all will be replaced by one; room is replacing values greater than or equal to 10 by 2, but since they are all 1 the result will be an empty list. Do the table test to better understand.

2 answers

0


Your code is experiencing several basic problems. For example: You are reading 20 numbers on for initial and then initializing 3 times in a row the variable numeros.

It’s right to do everything within one for and add 0, 1 or 2 according to some ifs. Below is a working code, but do not copy and paste only. As it’s a college job try to understand what’s going on, and the main thing: why didn’t yours work.

numeros = []

for i in range(20):
  x = int(input('digite um valor: '))
  if x <= 0:
    numeros.append(0)
  elif x < 10:
    numeros.append(1)
  else:
    numeros.append(2)

print(
    numeros
)

Here’s a excellent initial course of Python.

-1

Separate your problem into micro-problems, as @Woss commented. What you need to solve this problem?

  • Create an empty list
  • Request 20 times for the user to enter the numbers. What is the best way to do this?;
  • Every new number the user enters, put it in the list. Use append;
  • After the 20 iterations of the Voce user you will have your complete list;
  • Scroll through your newly created list, item by item. What’s the best way to do this?
  • As you go through your list, make conditions to change your values, as stated. (Here you can create a second list to save the new result or change the existing list);
  • Print the list with the new results to see.

----------------- or ------------------

  • Create an empty list
  • Request 20 times for the user to enter the numbers;
  • At each new number the user enters, make the necessary conditions to already record the typed number replaced by the statement logic;
  • Print the list with the new results to see.

In this second example, exactly what was stated was not done, for example. Because according to "ask the user for numbers and store them in a 20 position vector", it implies that Oce needs to create a list of the numbers typed by the user before converting them.

That’s more or less what you need to do. Of course each programmer can think of a different logic. Try to be part of it. And keep on researching what you can’t do little by little... Dividing a problem into several helps you better understand the logic you need to use..

Browser other questions tagged

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