Ask coding questions

← Back to all posts
how would i do this?
Dominicl645 (429)

help, how should i go with this?

Create a list elementsList that contains the names of the first 20 elements in atomic number order. Print the even number elements in the list with the atomic number all on one line. Output example: “2 – Helium, 4 – Beryllium, …”
elementsList = Hydrogen, Helium, Lithium, Beryllium, Boron, Carbon, Nitrogen, Oxygen, Fluorine, Neon, Sodium, Magnesium, Aluminum, Silicon, Phosphorus, Sulfur, Chlorine, Argon, Potassium, Calcium.

The elements60List contains the names of the first 60elements in atomic number order. Print the odd number elements in the list with the atomic number elements60List.
elements60List = 'Hydrogen', 'Helium', 'Lithium', 'Beryllium', 'Boron', 'Carbon', 'Nitrogen', 'Oxygen', 'Fluorine', 'Neon', 'Sodium', 'Magnesium', 'Aluminum', 'Silicon', 'Phosphorus', 'Sulfur', 'Chlorine', 'Argon', 'Potassium', 'Calcium', 'Hydrogen', 'Helium', 'Lithium', 'Beryllium', 'Boron', 'Carbon', 'Nitrogen', 'Oxygen', 'Fluorine',
'Neon', 'Sodium', 'Magnesium', 'Aluminum', 'Silicon', 'Phosphorus', 'Sulfur', 'Chlorine', 'Argon', 'Potassium', 'Calcium', 'Scandium', 'Titanium', 'Vanadium', 'Chromium', 'Manganese', 'Iron', 'Cobalt', 'Nickel', 'Copper', 'Zinc', 'Gallium', 'Germanium', 'Arsenic', 'Selenium', 'Bromine', 'Krypton', 'Rubidium', 'Strontium', 'Yttrium', 'Zirconium'

Answered by RYANTADIPARTHI (5872) [earned 5 cycles]
View Answer
Comments
hotnewtop
Dominicl645 (429)

any help would be greatly appreciated!

Dominicl645 (429)

would you know how i should go at this? @RYANTADIPARTHI

RYANTADIPARTHI (5872)

@dominicl645 uhh, this looks like homework, so I'm going to explain it to you:

  • make a list with all those values
  • to check even numbers do this
for i in range(1, 11)
  if i % 2 == 0:
     print(i)
  • and for odd, do this:
for i in range(1, 11):
  if i % 2 == 1:
    print(i)

That should work

Dominicl645 (429)

but how would i get it to print like this one "2 - helium" ? @RYANTADIPARTHI

Dominicl645 (429)

like to get it to print like it says in the example. @RYANTADIPARTHI

RYANTADIPARTHI (5872)

@dominicl645 then just print it - print('your value?')

or if it's a variable, do it without quotes.

Dominicl645 (429)

then how would i get it to change when it goes to the next element? like if it is print("2-", helium), how would i make the number go up by 2 and print another element without changing the print statement? cuz im trying to use it in a "for" loop, so if i change the print statement, then it will change everythings output. @RYANTADIPARTHI

Coder100 (15763)

something like:

elementsList = [...]

for i in range(len(elementsList)):
  print("%d -- %s" % (i, elementsList[i]))

and for the second one:

elements60List = [...]

for i in range(len(elements60List), 2):
  print("%d -- %s" % (i, elementsList[i]))

evidently you will need to actually put in the values inside the list.

Dominicl645 (429)

Traceback (most recent call last):
File "main.py", line 46, in <module>
print("d% -- %s" % (item, elementsList[item]))
ValueError: unsupported format character '%' (0x25) at index 6

this is what i get

@Coder100

Dominicl645 (429)

yeah, i eventually figured it out with a little help from @RYANTADIPARTHI @Coder100