Maynnnn
2020-11-05 21:22
采纳率: 40%
浏览 82
已采纳

Python, 请大神帮我看下,怎样做才能在执行完一个if语句后不退出程序,而是回到我写的菜单?

import sys
import numpy as np
from functions import *

print("|",format("\033[1;36m  Welcome to Bacteria Analyzing \033[0m","*^60"),"|")
print("\n Please select the action you want to take:")
print("\n 1. Load data")
print("\n 2. Filter data")
print("\n 3. Diaplay statistics")
print("\n 4. Generate plots")
print("\n 5. Quit")
    
user_option=int(input("The action you want to take: "))

data = np.loadtxt("valid_data.txt")



def filter_data():
        global bacteria
        bacteria=int(input("What kind of bacteria you want to analise? (Type in NA if you don't want to filter bacteria)  "))
        global groRate_l
        groRate_l=float(input("lower limit: "))
        global groRate_h
        groRate_h=float(input("higner limit: "))
        print(("The bacteria you want to check is""\033[1;35m {0}\033[0m").format(bacteria))
        print(("The growth range you want to check is ""\033[1;35m[{:f},{:f}]\033[0m").format(groRate_l,groRate_h))
        
        global data_f
        data_f=data[((data[:,1]>groRate_l) & (data[:,1]<groRate_h) & (data[:,2]==bacteria))]


if user_option==1:
    try:
        file_name=str(input("Pease enter your file's name: "))
        dataLoad(file_name)
    except:
        print("\033[1;31m File not found, plesae check your spelling.\033[0m")
        
if user_option==2:
    filter_data()

if user_option==3:
    statistic = str(input("which statistic you want to perform? "))
    dataStatistics(data_f, statistic)
    

if user_option==5:
    print("Thank you for using, bye~")
    sys.exit()
  

还有怎么样定义一个全局变量:

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • PythonJavaC++go 2020-11-06 01:32
    已采纳
    import sys
    import numpy as np
    from functions import *
     
    print("|",format("\033[1;36m  Welcome to Bacteria Analyzing \033[0m","*^60"),"|")
    print("\n Please select the action you want to take:")
    print("\n 1. Load data")
    print("\n 2. Filter data")
    print("\n 3. Diaplay statistics")
    print("\n 4. Generate plots")
    print("\n 5. Quit")
        
    user_option=int(input("The action you want to take: "))
     
    data = np.loadtxt("valid_data.txt")
     
     
     
    def filter_data():
            global bacteria
            bacteria=int(input("What kind of bacteria you want to analise? (Type in NA if you don't want to filter bacteria)  "))
            global groRate_l
            groRate_l=float(input("lower limit: "))
            global groRate_h
            groRate_h=float(input("higner limit: "))
            print(("The bacteria you want to check is""\033[1;35m {0}\033[0m").format(bacteria))
            print(("The growth range you want to check is ""\033[1;35m[{:f},{:f}]\033[0m").format(groRate_l,groRate_h))
            
            global data_f
            data_f=data[((data[:,1]>groRate_l) & (data[:,1]<groRate_h) & (data[:,2]==bacteria))]
     
    while True:
    	if user_option == 1:
    		try:
    			file_name = str(input("Pease enter your file's name: "))
    			dataLoad(file_name)
    		except:
    			print("\033[1;31m File not found, plesae check your spelling.\033[0m")
    	
    	if user_option == 2:
    		filter_data()
    	
    	if user_option == 3:
    		statistic = str(input("which statistic you want to perform? "))
    		dataStatistics(data_f, statistic)
    	
    	if user_option == 5:
    		print("Thank you for using, bye~")
    		sys.exit()

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题