[英]unexpected output at menu in my python program at raw_input prompt
下面是我收到的输出,下面是我的代码。 有什么原因可以获取此内存参考? 顺便说一句-缩进在我的程序中是正确的,但是我在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()
好的,根据您的评论,我将在后面详细介绍。 但是请注意,我将根据您的代码提供一个示例(有更好的方法来实现相同的结果,但是您只需咬一口并阅读文档即可)。
应用程序的高级流程是以下无限循环:待机屏幕->用户登录->主菜单->待机屏幕。 因此,这应该直接表示:
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
希望能有所帮助。
但是,仍然咬下去,阅读文档 :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.