python - 用户选择选项后,有什么方法可以将其链接回菜单
问题描述
有没有更简单的方法可以将选项循环回主菜单?我该怎么做?
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)
有没有更简单的方法可以将选项循环回主菜单?我该怎么做?
解决方案
欢迎来到 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()
推荐阅读
- flutter - 是否有一个功能可以打破文本并将其保存在容器内的下一行中?
- c++ - 带方括号的 for 循环
- splunk - SplunkHttp Appender 错误:ERROR 错误处理元素 SplunkHttp ([Appenders: null]): CLASS_NOT_FOUND
- python - Python-Networkx 图形生成函数 - For 循环
- wso2 - WSO2 - 在序列(SQ)文件的标题中使用本地条目配置变量
- python - 使用 open() 函数时,GUI 不会打开
- c++ - Gstreamer appsink 缓冲区不可写
- python - 函数内部的变量如何在python中的函数外部使用该变量
- java - 以编程方式从 Java 的 Batch 或 Shell 脚本创建可执行二进制文件
- python - 在 DataFrame 中将字符串转换为日期时间,同时忽略空字符串