import copy, sys
class MyApp(object):
message = '''
Choose:
1. Build a table
2. Encrypt
3. Decrypt
4. End
'''
opts = ['table', 'encrypt', 'decrypt', 'exit_']
def __init__(self):
self.at_menu = True
self.main()
def main(self):
while self.at_menu:
try:
choice = raw_input(MyApp.message)
# Executes the item in the list at the selected
# option -1 (since python is base 0 by default)
getattr(self, MyApp.opts[int(choice)-1])()
except (KeyError, ValueError, IndexError):
print 'Wrong choice'
except (KeyboardInterrupt, SystemExit):
self.exit_
def exit_(self):
self.at_menu=False
def table(self):
self.nc = int(raw_input('Input number of columns: '))
self.nr = int(raw_input('Input number of rows: '))
row = [None for i in range(0,self.nc+1)]
self.table = [copy.deepcopy(row) for i in range(0,self.nr+1)]
for ch in range (1,self.nc+1):
self.table[0][ch] = raw_input("Column Header {}: ".format(ch))
for rh in range(1,self.nr+1):
self.table[rh][0] = raw_input("Row Header {}: ".format(rh))
for i in range(1,self.nr+1):
for j in range(1,self.nc+1):
self.table[i][j] = raw_input("Data {}{}: ".format(i,j))
print str(self.table)
def encrypt(self):
#code using the class vars self.nc, self.nr, and etc
pass
def decrypt(self):
#code using the class vars self.nc, self.nr, and etc
pass
if __name__ == '__main__':
MyApp()
def get_option()
#code to request option, validate it is valid
return option
def table():
# Your table code
return tb, nc, nr
def encrypt( tb, nc, nr ):
# your code
def decrypt( tb, nc, nr ):
# your code
def main():
option = None
at_menu = true
table = None
nc = None
nr = None
while( at_menu ):
option = get_option()
if option == 1:
ret_tup = table()
table = ret_tup[0]
nc = ret_tup[1]
nr = ret_tup[2] # magic numbers bad
elif option == 2:
# should add code to confirm table is not None.
encrypt( table, nc, nr )
elif option == 3:
# should add code to confirm table is not None and it has been encrypted
decrypt( table, nc, nr )
elif option == 4:
at_menu = false
为了将变量从一个函数传递到另一个函数,它必须是“全局的”。一个简单的方法是在所有函数之外初始化变量,然后让所有函数调用它。这样,它将在所有函数中定义。你知道吗
这里有一个课堂例子,我充实了一些基础知识,随时可以适应你的需要。这里的密钥是所有“全局”变量(nc、nr和table)都被简单地存储为类属性,以便稍后由类成员函数访问(即加密、解密)。你知道吗
首先,您需要一个main函数来运行您正在做的事情。 该主函数将保存表、nc和nr的变量。 在主功能中:
-在菜单中调用变量。把它设为真。
-创建一个“while(at\u menu):”循环,它将始终返回到菜单。
-在while循环中输入代码以请求选项。
-使用单独的if/elif/else语句来捕获选项。
-然后,table()、encrypt()和decrypt()返回的值将重新分配main函数中的变量值。你知道吗
像这样:
相关问题 更多 >
编程相关推荐