Python菜单不工作,在ELIF处停止

2024-04-25 02:06:19 发布

您现在位置:Python中文网/ 问答频道 /正文

当我运行我的代码时,我在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)


Tags: 代码nameinfromimportforlinecounter
2条回答

问题在于缩进尝试:

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

Python对缩进很敏感一旦您取消缩进并且不使用elif或else语句,它将假定您已经完成了if语句

因此,编译器在到达elif时会感到困惑,因为它看不到与之相关联的if语句。因此,您必须缩进if语句和elif语句之间的所有行以修复错误,并缩进连续elif语句之间的所有行

相关问题 更多 >