根据python中的用户输入获取一行

时间:2014-03-07 18:08:04

标签: python-3.x

我是Python的新手,并且非常努力理解这门语言,因为我不是程序员,而是在努力学习。

我正在尝试创建一个简单的程序,它接受用户输入并从该行返回特定数据。

用户将输入用户ID和PIN#,然后按登录按钮。然后,两个文本框将显示用户检查和储蓄余额。

该文件的名称允许调用account.txt在选项卡提交的文本文件中包含以下数据:

userid     pin#    savingsbalance     checkingbalance

所以我的文件如下所示。我有5个用户:

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

我希望用户输入用户名和pin#,然后在2个单独的文本框中显示检查和保存余额。我正在努力理解的是如何从文本文件和该行的特定数据返回特定行。

我在下面创建了一个基本程序,打开文件进行读取并显示所有数据。我只需要它来返回登录用户的检查和储蓄余额。现在我在代码中没有文本框,但甚至不知道如何返回我需要的特定数据。

非常感谢任何帮助。

'''
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() 

这是我使用zell python 3书中的graphics.py和button.py代码的新代码:

来自图片导入* 来自按钮导入按钮

类ATM:     #这个类实现了一个简单的ATM

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 == '<-':
        # 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()

0 个答案:

没有答案