python - Python 菜单不起作用 - 在 ELIF 处停止
问题描述
当我运行我的代码时,ELIF 出现错误。每当我尝试提交此文件时,它都会在 elif 处给我一个无效的语法错误。我已经阅读了有关此的不同主题,但那些 elif 缩进得太远了。
我当前的代码:
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)
解决方案
问题在于缩进尝试:
import turtle
from collections import Counter
print (30 * '-')
print (" M A I N - M E N U")
print (30 * '-')
print ("1.Total Populations of the different continents")
print ("2.Percentage Change(%) for different countries")
print ("3. Continent with the lowest Population")
print ("4. Choose a country")
print (30 * '-')
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)
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==3:
#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()
else:
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()
推荐阅读
- javascript - 如何将类分配给“new () =>”类型的属性
- css - 如何在 Ionic 3 中添加自定义 CSS
- html - 样式化镀铬物扩展按钮
- laravel - 使用组和中间件 auth:api 在 laravel 5.6 的控制器中通过路由 api.php 获取用户 ID
- java - 如何获取所有临时节点以及它们在 Zookeeper 服务器中存在多长时间?
- php - 在 null 上调用成员函数 have_posts()
- c++ - 在函数中传递带有指针成员的结构是 C 中的浅拷贝或深拷贝
- typescript - import mathjax 抛出错误 @types/mathjax/index.d.ts 不是模块
- ios - 用于电子邮件/密码的 Swift 4 身份验证
- git - Android Kotlin 项目未在 Git 上上传