首页 > 解决方案 > 用户选择选项后,有什么方法可以将其链接回菜单

问题描述

有没有更简单的方法可以将选项循环回主菜单?我该怎么做?

from collections import Counter 
print ("   M A I N - M E N U")
print ("1.People")
print ("2.Name")
print ("3. Country")
print ("4. Continent")
opt = int(input("Enter option: "))
if opt ==1:
  print ("People")
from collections import Counter 
counterY = Counter()
with open('json.txt') as f:
        for i in range(0,2):
            next(f)
        for line in f:
            splits = line.split(';')
            people = int(splits[3])
            counter1[name] += people
for name, pop_sum in counter1.most_common():
    print(Name, ":", pop_sum)
elif opt == 2:
  from collections import Counter 
counterx = Counter()
with open("json.txt") as f:
    for i in range(0,2):
            next(f)
    for line in f:
        splits = line.split(';')
        change = float(splits[6])
        country = splits[1].strip()
        counter2[country] += change      
#Percentage Change By Countries"
print()
print ("Countries"):
    print(country)

有没有更简单的方法可以将选项循环回主菜单?我该怎么做?

标签: pythonpython-3.x

解决方案


欢迎来到 StackOverflow!

稍等片刻(真)环绕您的代码,如果一个选项与 4 匹配,它将结束程序,否则它将再次打印主菜单。

PS:我将您的主菜单放在一个函数中,以便您的代码更具可读性。

import turtle
from collections import Counter 

def main_menu():
    print(30 * '-')
    print("   M A I N - M E N U")
    print(30 * '-')
    print("1.Total Populations of the different continents & Continent with the lowest Population")
    print("2.Percentage Change(%) for different countries")
    print("3. Choose a country")
    print("4. Exit")
    print(30 * '-')

while(True):
    main_menu()
    opt = int(input("Enter option: "))
    if opt ==1:
        print("Continents Sorted By Population")
        from collections import Counter 
        counter1 = Counter()
        with open('demo.txt') as f:
            for i in range(0,2):
                next(f)
            for line in f:
                splits = line.split(';')
                population = int(splits[3])
                continent = splits[-1].strip()
                counter1[continent] += population
            # Print continents sorted by population
            for continent, pop_sum in counter1.most_common():
                print(continent, ":", pop_sum)
     #Finding continent with lowest population
        counter3 = Counter()
        with open("demo.txt") as f:
            for i in range(0,2):
                next(f)
            for line in f:
                counter3[continent] += population
        for continent, pop_total in counter3.most_common():
            print()
            print("Continent with the Lowest Population")
            print(continent,":", pop_sum)
            print()
    elif opt == 2:
        from collections import Counter
        counter2 = Counter()
        with open("demo.txt") as f:
            for i in range(0,2):
                next(f)
            for line in f:
                splits = line.split(';')
                change = float(splits[6])
                country = splits[1].strip()
                counter2[country] += change      
        #Percentage Change By Countries"
        print()
        print("Percentage Change By Countries")
        for country, change_sum in counter2.most_common():
            print(country, change_sum,"%")
    elif opt == 4:
        break

def file_search():
        userInput = input('Enter a country: ').lower()
        result = []
        with open("demo.txt", 'r') as f:
            for x in f:
                if userInput in x.lower():
                    result.append(x.split(';'))
        for s in result:
            print(s[1] + " \nCountry Rank: "+ s[0]+ " \n2019 population: " + s[3] + "\nPopulation change(%)"+s[6]+"\nContinent:  "+ s[7])
file_search()

推荐阅读