Purpose To practice creating and using tuples Degree of Diff

Purpose: To practice creating and using tuples. Degree of Difficulty: Easy In this exercise, you will create a program that can display information about the first ten elements in the periodic table. The periodic table of is a table of the chemical elements. It is organized by the element\'s atomic number (the number of protons the element has) and contains information about each element, such as atomic weight. Create a list of tuples that holds the information about the first 10 elements in the periodic table (just write the list of tuples as a literal, do not try to input it from the console). Each tuple will include: the atomic symbol, name, number, and weight. The data for each element is provided in the table below. Now your program should prompt the user to enter an atomic number between 1 and 10 from the console. The program will then display information about the element with that atomic number on the console. Sample Output Here is an example of what your program\'s console output should look like. Green text was entered by the user. Periodic Table Information Which element would you like to know about? Enter the atomic number now: 7 Symbol: N Name: Nitrogen Number: 7 Weight: 14.007

Solution

CODE:

# Create two lists - lt and gl.
lt = list()
gl = list()

# using the extend functionality include all the elements into the list.
lt.extend([\'H\',\'Hydrogen\',1,1.008])
lt.extend([\'He\',\'Helium\',2,4.003])
lt.extend([\'Li\',\'Lithium\',3,6.940])
lt.extend([\'Be\',\'Beryllium\',4,9.012])
lt.extend([\'B\',\'Boron\',5,10.810])
lt.extend([\'C\',\'Carbon\',6,12.011])
lt.extend([\'N\',\'Nitrogen\',7,14.007])
lt.extend([\'O\',\'Oxygen\',8,15.999])
lt.extend([\'F\',\'Fluorine\',9,18.998])
lt.extend([\'Ne\',\'Neon\',10,20.180])

# Create an iterator to the list.
it = iter(lt)

# zip the iterator with itself to create List of tuples
gl = zip(it,it,it,it)

print \"Periodic Table Information\"
print \'Which element you would like to know about?\'

# Take the input from the user.
# For python 2.7, use raw_input instead of input.
num = int(input(\'Enter the atomic number now:\'))

# Iterate through the list of tuples and display the result.
for item in gl:
   if num == item[2]:
       print \'Symbol: \', item[0]
       print \'Name: \', item[1]
       print \'Number: \', item[2]
       print \'Weight: \', item[3]
       break
  
  
OUTPUT:
> python test.py
Periodic Table Information
Which element you would like to know about?
Enter the atomic number now:6
Symbol: C
Name: Carbon
Number: 6
Weight: 12.011

 Purpose: To practice creating and using tuples. Degree of Difficulty: Easy In this exercise, you will create a program that can display information about the f
 Purpose: To practice creating and using tuples. Degree of Difficulty: Easy In this exercise, you will create a program that can display information about the f

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site