title: Get a line based on user input in python
body: <p>I am very new to Python and really struggling to understand the language as I am not a programmer but trying to learn.</p>
  
  <p>I am trying to create a simple program that takes a users input and returns specific data from the line.</p>
  
  <p>The user would enter a userid and a pin#, and press login button. Two text boxes would then display the users checking and savings balance. </p>
  
  <p>The name of the file lets call account.txt contains the following data in a tab delmited text file:</p>
  
  <pre><code>userid     pin#    savingsbalance     checkingbalance
  </code></pre>
  
  <p>So my file looks like the below. I have 5 users:</p>
  
  <pre><code>dwarner 2013    845.34  10944.44
  
  kwarner 2014    844.23  5478.32
  
  jwarner 2015    239.00  4593.44
  
  ewarner 2016    943.22  9325.76
  
  fwarner 2017    345.987 8763.65
  </code></pre>
  
  <p>I would like to have the user enter a username and pin#, then display the checking and saving balance in 2 separate text boxes. What I am struggling to understand is how to return a specific line from a text file and specific data from that line. </p>
  
  <p>I have created a basic program below that opens the file for read and displays all the data. I just need it to return the checking and savings balance of the user that logged in. Right now I do not have text boxes in the code but dont even know how to return the specific data I need.</p>
  
  <p>Any help would be greatly appreciated.</p>
  
  <pre><code>'''
  Created on Feb 12, 2014
  
  @author: Dirk
  '''
  
  
  def readname(filename):
      infile = open(filename, 'r')
      for line in infile:
          print(line)
  
      #return name
  
  def main():
      filename = "/home/ia5040/workspace/IA5040PythonSamples/IA5040SamplesPackage/atm.txt"
      name = readname(filename)
  
  
  main() 
  </code></pre>
  
  <p>Here is my new code using the graphics.py and button.py code from the zell python 3 book:</p>
  
  <p>from graphics import *
  from button import Button</p>
  
  <p>class ATM:
      # This class implements a simple ATM</p>
  
  <pre><code>def __init__(self):
      # create the window for the ATM
      win = GraphWin("Money Magic", 600, 480)
      win.setCoords(0,0,10,10)
      win.setBackground("grey")
      self.win = win
      # Now create the widgets
      self.__createButtons()
      self.__createDisplay()
      self.__createDisplay1()
      self.__createDisplay2()
      self.__createDisplay3()
  
  def __createButtons(self):
      # create list of buttons
      # start with all the standard sized buttons
      # bSpecs gives center coords and label of buttons
      bSpecs = [(1.2,7,'Login'), (3.2,7,'Logout'),
                (1.2,5,'Exit'), (3.2,5,'Withdraw')]
      self.buttons = []
      for (cx,cy,label) in bSpecs:
          self.buttons.append(Button(self.win,Point(cx,cy),1.5,.50,label))
      # create the larger = button
      #self.buttons.append(Button(self.win, Point(4.5,1), 1.75, .75, "="))
      # activate all buttons
      for b in self.buttons:
          b.activate()
  def __createDisplay(self):
      bg = Rectangle(Point(.5,8.5), Point(2.0,9))
      bg.setFill('white')
      bg.draw(self.win)
      text = Text(Point(1,6), "")
      text.draw(self.win)
      label = Text(Point(1.3,8.2), "User ID")
      label.draw(self.win)
      text.setFace("courier")
      text.`enter code here`setStyle("bold")
      text.setSize(16)
      self.display = text
  
  def __createDisplay1(self):
      bg = Rectangle(Point(2.5,8.5), Point(4.0,9))
      bg.setFill('white')
      bg.draw(self.win)
      text = Text(Point(1,6), "")
      text.draw(self.win)
      label = Text(Point(3.3,8.2), "Pin #")
      label.draw(self.win)
      text.setFace("courier")
      text.setStyle("bold")
      text.setSize(16)
      self.display = text
  
  def __createDisplay2(self):
      bg = Rectangle(Point(6,8.5), Point(8,9))
      bg.setFill('white')
      bg.draw(self.win)
      text = Text(Point(1,6), "")
      text.draw(self.win)
      label = Text(Point(7,8.2), "Checking")
      label.draw(self.win)
      text.setFace("courier")
      text.setStyle("bold")
      text.setSize(16)
      self.display = text
  
  def __createDisplay3(self):
      bg = Rectangle(Point(6,6.5), Point(8,7))
      bg.setFill('white')
      bg.draw(self.win)
      text = Text(Point(1,6), "")
      text.draw(self.win)
      label = Text(Point(7,6.2), "Savings")
      label.draw(self.win)
      text.setFace("courier")
      text.setStyle("bold")
      text.setSize(16)
      self.display = text
  
  def getButton(self):
      # Waits for a button to be clicked and returns the label of
      #    the button that was clicked.
      while True:
          p = self.win.getMouse()
          for b in self.buttons:
              if b.clicked(p):
                  return b.getLabel() # method exit
  
  def processButton(self, key):
      # Updates the display of the calculator for press of this key
      text = self.display.getText()
      if key == 'C':
          self.display.setText("")
      elif key == '&lt;-':
          # Backspace, slice off the last character.
          self.display.setText(text[:-1])
      elif key == '=':
          # Evaluate the expresssion and display the result.
          # the try...except mechanism "catches" errors in the
          # formula being evaluated.
          try:
              result = eval(text)
          except:
              result = 'ERROR'
          self.display.setText(str(result))
      else:
          # Normal key press, append it to the end of the display
          self.display.setText(text+key)
  
  def run(self):
      # Infinite 'event loop' to process button clicks.
      while True:
          key = self.getButton()
          self.processButton(key)
  
  # This runs the program.
  if __name__ == '__main__':
  # First create a ATM object
  theATM = ATM()
  # Now call the ATM's run method.
  theATM.run()
  </code></pre>