0

下面是我收到的输出,下面是我的代码。为什么我得到这个内存参考有什么原因?顺便说一句 - 缩进在我的程序中是正确的,但我很难在 stackoverflow 中发布它

>>>Welcome to the Main Menu, Richard.
>>>Please select from the following options:
    >>>(1)Punch In
    >>>(2)Punch Out
    >>>(3)Exit
>>>(type 1, 2, or 3)   
>>><__main__.PunchCard instance at 0x7f5c2b799ea8>

和代码

import xlrd
import sys
data = xlrd.open_workbook('data.xls')
sheetname = data.sheet_names()
employee_sheet = data.sheet_by_index(0)

uid_list = [c.value for c in employee_sheet.col(0)]
last_list = [c.value for c in employee_sheet.col(1)]
first_list = [c.value for c in employee_sheet.col(2)]
username_list = [c.value for c in employee_sheet.col(3)]
password_list = [c.value for c in employee_sheet.col(4)]

class PunchCard:

    def idle_screen(self):
        sys.stderr.write("\x1b[2J\x1b[H")
        print "Press Enter to start PunchClock"
        raw_input()
        return self.user_login()

    def user_login(self):
        sys.stderr.write("\x1b[2J\x1b[H")
        userinput = raw_input("Please enter your username.\n> ")
        if userinput in username_list:
                user_index = username_list.index(userinput)
                self.user_first = first_list[user_index]
                user_password = raw_input("Welcome %s, please enter your password.\n> " % self.user_first)
        else:
                print "That is an incorrect username."
                raw_input("Press enter to re-enter your username.")
                return self.user_login()

        if user_password == password_list[user_index]:
                return self.main_menu()
        else:
                sys.stderr.write("\x1b[2J\x1b[H")
                print "You have entered an incorrect password.\nPress enter to try again, or type QUIT to return to previous menu."
                raw_input()
                return self.user_login()

    def main_menu(self):        
            sys.stderr.write("\x1b[2J\x1b[H")
            print "Welcome to the Main Menu, %s.\nPlease select from the following options:\n    (1)Punch In\n    (2)Punch Out\n    (3)Exit\n\n(type 1, 2, or 3)" % self.user_first 
            menu_input = raw_input(self)
            if menu_input == '1':
                print "punched in"
                raw_input("You clocked in at XX. Press enter to continue.")
                return self.main_menu()
            elif menu_input == '2':
                print "punched out"
                raw_input("You clocked out at XX. Press enter to continue.")
                return self.main_menu()
            elif menu_input == '3':
                return self.idle_screen()
            else:
                return self.main_menu()

s = PunchCard()
s.idle_screen()
4

1 回答 1

0

好的,根据您的评论,我会更详细地回答。但请注意,我提供了一个基于您的代码的示例(有更好的方法可以达到相同的结果,但您只需认真阅读文档)。

您的应用程序的高级流程是以下无限循环:空闲屏幕 -> 用户登录 -> 主菜单 -> 空闲屏幕。所以这应该直接表达:

s = PunchCard()
while True:
    s.idle_screen()
    s.user_login()
    s.main_menu()

然后,idle_screen应该只是等待输入并返回(退出方法):

def idle_screen(self):
    sys.stderr.write("\x1b[2J\x1b[H")
    print "Press Enter to start PunchClock"
    raw_input()
    # method returns to caller at this point

user_login()应该循环直到发生有效登录并返回(退出方法):

def user_login(self):
    While True:
        sys.stderr.write("\x1b[2J\x1b[H")
        userinput = raw_input("Please enter your username.\n> ")
        if userinput in username_list:
            user_index = username_list.index(userinput)
            self.user_first = first_list[user_index]
            user_password = raw_input("Welcome %s, please enter your password.\n> " % self.user_first)
    else:
            print "That is an incorrect username."
            raw_input("Press enter to re-enter your username.")
            continue # start of the beginning of the loop again

    if user_password == password_list[user_index]:
            return # exit method
    else:
            sys.stderr.write("\x1b[2J\x1b[H")
            print "You have entered an incorrect password.\nPress enter to try again, or type QUIT to return to previous menu."
            raw_input()
            # do nothing here (will loop again)
            # Note your prompt mentions "QUIT" but you don't handle it ;)
            # Current logic means the login loop continues until a valid login occurs

最后,main_menu循环直到用户退出并且方法返回(一切都从顶层循环开始):

def main_menu(self):        
    While True:
        sys.stderr.write("\x1b[2J\x1b[H")
        print "Welcome to the Main Menu, %s.\nPlease select from the following options:\n    (1)Punch In\n    (2)Punch Out\n    (3)Exit\n\n(type 1, 2, or 3)" % self.user_first 
        menu_input = raw_input(self)
        if menu_input == '1':
            print "punched in"
            raw_input("You clocked in at XX. Press enter to continue.")
        elif menu_input == '2':
            print "punched out"
            raw_input("You clocked out at XX. Press enter to continue.")
        elif menu_input == '3':
            return

希望有帮助。

但是,请咬紧牙关阅读文档:)

于 2013-01-31T16:59:51.480 回答