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入力と戻りを待つだけです(exitメソッド):

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()有効なログインが発生して戻るまでループする必要があります (exit メソッド):

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 に答える