Filling a list using its size as a stop criterion

Asked

Viewed 174 times

8

I’m creating a code that defines a function that depends on two variables (Z and N).

This program defines the sum of these variables and attaches each possible sum in an empty list.

The list must have 118 elements, for that I created an empty list and I am attaching the values of the sum in the list one at a time, the program will run 118 times until the list reaches the size mentioned.

However I am not able to compile this code here, someone could help me find the error?

#Este programa cria uma classe chamada Número Atômico
class NumeroAtômico:

#Definindo a função Numero Atômico
    def A(Z,N):

    #Para isso, vamos completar a lista com todos os números atômicos
    # A lista que contem cada número atômico está aqui

        ListadeNumerosAtomicos=[]

        while len(ListadeNumerosAtomicos!=118):

            # O número de prótons Z é o número de prótons existentes no núcleo de um átomo
            print("")
            Z=int(input())

             # O número de neutrons N é o número de prótons existentes no núcleo de um átomo
            print("")
            N=int(input())

            # O número de massa A é a soma do número de prótons (Z) e de nêutrons (N) existentes num átomo

            A = Z + N


            if (A>=1) and (A<=118):
                print ("")
                print("")
                ListadeNumerosAtomicos.append(A)
                print(ListadeNumerosAtomicos)

            A(Z,N)
  • Note that the atomic number of an atom (or ion) is equal to its number of protons. The mass number is the sum of the number of protons with the number of neutrons (i.e., it is equal to the number of nucleons). A ListadeNumerosAtomicos should contain atomic numbers and not mass numbers. The atomic number is Z and the mass number is A, in your program.

  • It looks like you’re trying to add mass numbers to a list that supposedly should contain atomic numbers.

2 answers

13


General response:

Use:

for i in range(numero_de_elementos_que_a_sua_lista_deve_ter):
  funcoes_opcionais_que_alteram_o_elemento_a_adicionar()
  a_sua_lista.append(elemento_a_adicionar)

See the documentation of range or range Python for more information.

Specific answer:

Note that the atomic number of a atom (or ion) is equal to its number of protons, at all times. The mass number is equal to the sum of the number of protons with the number of neutrons (is equal to the number of nucleons).

On your show, you write if (A>=1) and (A<=118): and this does not allow to obtain the expected results, because nothing says that the mass number A belongs to the intermission [1, 118]. It seems that you are confusing the mass number with the atomic number... I strongly advise you to see: atomic number and mass number.

To ListadeNumerosAtomicos should contain atomic numbers and not mass numbers. The atomic number is Z and the mass number is A, in your program.

Atomic numbers are always integers and so far vary between 1 and 118, inclusive. The list of atomic numbers of known elements is equal to list(range(1, 119)), python. It looks like you are trying to add mass numbers to a list called ListadeNumerosAtomicos. Did you want to make a list of mass numbers?

Try the following, to get a list of mass numbers:

def devolve_lista_numeros_de_massa():
  lista_de_numeros_de_massa = []
  for i in range(118):
    Z = int(input("Insira o número atómico: "))  # número de protões
    # O número atómico é o número de protões, SEMPRE!
    N = int(input("Insira o número de neutrões: "))  # número de neutrões
    lista_de_numeros_de_massa.append(N+Z)
  return lista_de_numeros_de_massa

print(devolve_lista_numeros_de_massa())

Or, in a "line":

def devolve_lista_numeros_de_massa():
  return [int(input("Insira o número atómico: "))
          + int(input("Insira o número de neutrões: "))
          for i in range(118)]

print(devolve_lista_numeros_de_massa())

If the elements are introduced in ascending order of atomic number: first hydrogen, then helium... you only have to write the following:

def devolve_lista_numeros_de_massa():
  lista_de_numeros_de_massa = []
  for Z in range(1, 119):
    N = int(input("Insira o número de neutrões do elemento com Z=%d: " % Z))
    lista_de_numeros_de_massa.append(N+Z)
  return lista_de_numeros_de_massa

print(devolve_lista_numeros_de_massa())

Or, in a "line":

def devolve_lista_numeros_de_massa():
  return [int(input("Insira o número de neutrões do elemento com Z=%d:" % Z))
          + Z for Z in range(1, 119)]

print(devolve_lista_numeros_de_massa())

-1

use len(list)

example

for i in range(0, len(list) + 1)

Browser other questions tagged

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