首页 > 解决方案 > 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)

标签: pythonpython-3.x

解决方案


问题在于缩进尝试:

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

推荐阅读