Python - 在菜单中重用用户输入
怎么才能最好地实现这个功能 - 在菜单中重复使用用户输入呢?下面是我的示例代码:
def input():
user = raw_input('user: ')
passwd = getpass.getpass('passwd: ')
return (user,passwd)
def function1():
user, passwd, vcenter = input()
do something
def function2():
user, passwd, vcenter = input()
do something
def function3():
user, passwd, vcenter = input()
do something
def main():
while True:
if choice == 1:
function1()
elif choice == 2:
function2()
elif choice == 3:
function3()
else:
print 'Choose from the options only!'
break
1 个回答
0
我明白你的意思了,你是想在while循环中只用一次input(),而不是在每个函数里都重复调用它。如果是这样的话,我建议你可以试试这个方法:
def input():
user = raw_input('user: ')
passwd = getpass.getpass('passwd: ')
return (user,passwd)
def function1(param):
user, passwd = param
do something
def function2(param):
user, passwd = param
do something
def function3(param):
user, passwd = param
do something
def main():
while True:
if choice in [1,2,3]:
param = input()
if choice == 1:
function1(param)
elif choice == 2:
function2(param)
elif choice == 3:
function3(param)
else:
print 'Choose from the options only (1, 2, 3): '
break
希望这对你有帮助。