variable creation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alistair King

    #1

    variable creation

    Hei all,

    im trying to create a list of variables for further use:


    Els = raw_input("Are there any further elements you would like to
    include? if so type the element, eg, Pd. If not type No: ")

    if Els != 'No':
    el = Els in pt
    while el == 1 and Els != 'Next':
    elements = []
    elements.insert (-1, Els)

    Els = raw_input("Whic h further element would you like to
    include, eg, Pd, if not type Next: ")
    el = Els in pt

    while el == 0 and Els != 'Next':
    Els = raw_input("This is not an element in the periodic
    table, please try again, eg Pd, If you dont wish to include any more,
    type Next: ")
    if el == 1:
    elements.insert (-1, Els)

    while el == 0:
    Els = raw_input("This is not an element in the periodic
    table, please try again, eg Pd. If you dont wish to include any more,
    type Next: ")

    print elements


    this works to a certain extent but gets stuck on some loop. Im a
    beginner and am not sure where im going wrong.

    here is a portion of the dictionary containing typical elements:

    pt = {'H': 1.00794, 'He': 4.002602, 'Li': 6.941, 'Be': 9.012182, 'B':
    10.811}


    Ali


    --
    Dr. Alistair King
    Research Chemist,
    Laboratory of Organic Chemistry,
    Department of Chemistry,
    Faculty of Science
    P.O. Box 55 (A.I. Virtasen aukio 1)
    FIN-00014 University of Helsinki
    Tel. +358 9 191 50429, Mobile +358 (0)50 5279446
    Fax +358 9 191 50366

  • Justin  Azoff

    #2
    Re: variable creation

    Alistair King wrote:
    Hei all,
    >
    im trying to create a list of variables for further use:
    [snip]
    this works to a certain extent but gets stuck on some loop. Im a
    beginner and am not sure where im going wrong.
    You are trying to do too much in one function. Split those loops up
    into a few little ones and the program will work... or if it doesn't,
    you'll know exactly where the problem is.

    def get_element(pt) :
    "Return None or a single element from the periodic table"
    while 1:
    el = raw_input("Whic h element would you like to include? ")
    if not el: #blank answer
    return
    if el in pt:
    return pt[el]
    print "This element is not in the periodic table, please try
    again"

    def get_elements(pt ):
    elements = []
    while 1:
    el = get_element(pt)
    if not el:
    break
    elements.append (el)

    return elements

    See how using two separate functions makes it easy to test?
    In [10]:print get_element(pt)
    Which element would you like to include? X
    This element is not in the periodic table, please try again
    Which element would you like to include? H
    1.0079400000000 001

    In [11]:print get_elements(pt )
    Which element would you like to include? Z
    This element is not in the periodic table, please try again
    Which element would you like to include? Li
    Which element would you like to include? B
    Which element would you like to include? H
    Which element would you like to include?
    [6.9409999999999 998, 10.811, 1.0079400000000 001]


    Now, since the information for a single element consists of more than
    just a single number, you'll probably want to make a class for them.
    Once you have an object for every element, you can add them to a class
    for the periodic table.

    --
    - Justin

    Comment

    • Jon

      #3
      Re: variable creation

      Hi,
      I'm not sure if this is exactly what you had in mind but what about
      something like this:

      elements = []
      Els = ""
      pt = {'H': 1.00794, 'He': 4.002602, 'Li': 6.941, 'Be': 9.012182,
      'B':10.811}
      while Els != 'No':
      Els = raw_input("""Ar e there any further elements you would like to
      include? if so type the element, eg, Pd. If not type No:""")
      if pt.has_key(Els) :
      elements.append ({Els:pt[Els]})

      print elements


      Alistair King wrote:
      Hei all,
      >
      im trying to create a list of variables for further use:
      >
      >
      Els = raw_input("Are there any further elements you would like to
      include? if so type the element, eg, Pd. If not type No: ")
      >
      if Els != 'No':
      el = Els in pt
      while el == 1 and Els != 'Next':
      elements = []
      elements.insert (-1, Els)
      >
      Els = raw_input("Whic h further element would you like to
      include, eg, Pd, if not type Next: ")
      el = Els in pt
      >
      while el == 0 and Els != 'Next':
      Els = raw_input("This is not an element in the periodic
      table, please try again, eg Pd, If you dont wish to include any more,
      type Next: ")
      if el == 1:
      elements.insert (-1, Els)
      >
      while el == 0:
      Els = raw_input("This is not an element in the periodic
      table, please try again, eg Pd. If you dont wish to include any more,
      type Next: ")
      >
      print elements
      >
      >
      this works to a certain extent but gets stuck on some loop. Im a
      beginner and am not sure where im going wrong.
      >
      here is a portion of the dictionary containing typical elements:
      >
      pt = {'H': 1.00794, 'He': 4.002602, 'Li': 6.941, 'Be': 9.012182, 'B':
      10.811}
      >
      >
      Ali
      >
      >
      --
      Dr. Alistair King
      Research Chemist,
      Laboratory of Organic Chemistry,
      Department of Chemistry,
      Faculty of Science
      P.O. Box 55 (A.I. Virtasen aukio 1)
      FIN-00014 University of Helsinki
      Tel. +358 9 191 50429, Mobile +358 (0)50 5279446
      Fax +358 9 191 50366

      Comment

      Working...