【问题标题】:Trying to get the user to choose an operation they want to use and then run the respective program试图让用户选择他们想要使用的操作,然后运行相应的程序
【发布时间】:2020-03-15 03:22:42
【问题描述】:

试图让用户选择他们想要使用的操作,然后运行相应的程序。不明白这里有什么问题?

这是我的代码

from collections import Counter
print (30 * '-')
print ("   M A I N - M E N U")
print (30 * '-')
print ("Program 1.Total Populations of the different continents")
print ("Program 2.Percentage Change(%) for different countries")
print (30 * '-')

Input1=1
Input2=2

## Get input ###
choice = int(input('Enter your choice [1-2] : '))

### Take action as per selected menu-option ###
while choice==1 or choice==2:
    if (choice == 1):
        counter1 = Counter()
    with open('a.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)

    else:
        counter2 = Counter()
    with open("a.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,"%")






样本数据 来自联合国的 2019 年世界人口数据 秩;国家; 2018; 2019; % 分享;流行变化; % 改变;大陆 1个;中国; 1427647786; 1433783686; 18.6; 6135900; 0.43;亚洲 2;印度; 1352642280; 1366417754; 17.7; 13775474; 1.02;亚洲 3;美国; 327096265; 329064917; 4.27; 1968652; 0.60;北美 4;印度尼西亚; 267670543; 270625568; 3.51; 2955025; 1.10;亚洲 5个;巴基斯坦; 212228286; 216565318; 2.81; 4337032; 2.04;亚洲 6;巴西; 209469323; 211049527; 2.74; 1580204; 0.75;南美洲 7;尼日利亚; 195874683; 200963599; 2.61; 5088916; 2.60;非洲 8个;孟加拉国; 161376708; 163046161; 2.11; 1669453; 1.03;亚洲 9;俄罗斯联邦; 145734038; 145872256; 1.89; 138218; 0.09;欧洲 10个;墨西哥; 126190788; 127575529; 1.65; 1384741; 1.10;北美

【问题讨论】:

    标签: python python-3.x menu


    【解决方案1】:

    首先,我并没有真正理解你想要什么,但我纠正了你的缩进问题。

    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 (30 * '-')
    
    ## Get input ###
    choice = int(input('Enter your choice [1-2] : '))
    
    ### Take action as per selected menu-option ###
    
    #1st Operation
    print ("Continents Sorted By Population")
    while choice==1 or choice==2:
        if (choice == 1):
            counter1 = Counter()
            with open('a.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)
    
    #2nd operation
    #Percentage Change By Countries
        elif choice==2:
            counter2 = Counter()
            with open("a.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
    print()
    
    print ("Percentage Change By Countries")
    for country, change_sum in counter2.most_common():
        print(country, change_sum,"%")
    

    【讨论】:

    • 嗨,我刚刚编辑了我的原始代码,希望它有助于解决我的问题...谢谢顺便说一句!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多