Maynnnn 2020-11-05 21:22 采纳率: 50%
浏览 113
已采纳

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()

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程