这是我的代码:
from datetime import datetime
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import numpy as np
import re
import time
print('MAIN MENU')
print('1. Plot Graph A')
print('2. Plot Graph B')
print('3. Plot Graph C')
print('4. History')
print('5. Exit/Quit')
choice=raw_input('Please choose an option')
if choice == '1':
import plotA
elif choice == '2':
import plotB
elif choice == '3':
import plotC
elif choice == '4':
import history
elif choice == '5':
exit()
elif choice!='':
print('Try Again')
所以当我运行模块时,我可以输入我的选择,例如。我完成后输入'4',你应该能够再次输入你的选择,但它不能
我知道你不能只是导入它,但我不知道其他方式这样做
提前感谢:)
答案 0 :(得分:2)
如果您想再次遍历选项,可以使用while循环:
while True:
choice=raw_input('Please choose an option')
if choice == '1':
import plotA
elif choice == '2':
import plotB
elif choice == '3':
import plotC
elif choice == '4':
import history
elif choice == '5':
exit() # or just break
elif choice!='':
print('Try Again')
答案 1 :(得分:1)
Ashoka Lella的回复很好,但不要忘记break
。
while True:
choice=raw_input('Please choose an option')
if choice == '1':
import plotA
break
elif choice == '2':
import plotB
break
elif choice == '3':
import plotC
break
elif choice == '4':
import history
break
elif choice == '5':
exit() # or just break
elif choice!='':
print('Try Again')
答案 2 :(得分:-1)
也许你想要这个?
from datetime import datetime
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import numpy as np
import re
import time
def get_choice():
print('MAIN MENU')
print('1. Plot Graph A')
print('2. Plot Graph B')
print('3. Plot Graph C')
print('4. History')
print('5. Exit/Quit')
choice = raw_input('Please choose an option: ')
if choice == '1':
import plotA
elif choice == '2':
import plotB
elif choice == '3':
import plotC
elif choice == '4':
import history
elif choice == '5':
exit()
else:
print('Try Again')
get_choice()
if __name__ == '__main__':
get_choice()
您可以随时随地拨打电话。例如:
elif choice == '4':
import history
get_choice()
如果您不想使用递归函数;
from datetime import datetime
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import numpy as np
import re
import time
def get_choice():
print('MAIN MENU')
print('1. Plot Graph A')
print('2. Plot Graph B')
print('3. Plot Graph C')
print('4. History')
print('5. Exit/Quit')
choice = raw_input('Please choose an option: ')
if choice == '1':
import plotA
elif choice == '2':
import plotB
elif choice == '3':
import plotC
elif choice == '4':
import history
elif choice == '5':
exit()
else:
print('Try Again')
return False
return True
if __name__ == '__main__':
while True:
if get_choice() is True:
break