语法错误 - 突出显示变量Python

时间:2014-12-07 20:56:29

标签: python variables syntax

我是Python编码的初学者。我正在编写这段代码,该代码应该让用户在几张表中看到世界人口信息,然后有几个选项只是具有最高,最低男女比例等的句子。

我遇到并修复了多个错误。我似乎无法修复的最后一个错误只是语法错误,但我无法弄清楚出了什么问题。它会弹出一个表示语法错误的框,并突出显示我的本地变量bothSexes_total。显示的选项打印一个表格。

def main():
    print("Welcome user.") #greetings
    Ages0_14= open("Ages0-14.txt", "r") #open files needed
    Ages15_64= open("Ages15-64.txt", "r")
    Ages65= open("Ages65.txt", "r")
    countries, males0_14, females0_14= get_lists(Ages0_14) #makes 3 lists from 1st file
    empty, males15_64, females15_64= get_lists(Ages15_64) #2 lists from 2nd file
    empty2, males65, females65= get_lists(Ages65) #2 lists from 3rd file
    print_menu()
    choice= input("Type the letter of the choice you would like to make.") #user makes choice

    if choice== "A" or choice== "a": #option a: display country's information
        country_input= input("Choose a country you would like to know the population information of.")
        country=(countries.index(country_input) #find index of country

        # I get an error here:
        bothSexes_total=(int(females0_14[country])+ int(females15_64[country])+ int(females65[country])+ int(males0_14[country])+ int(males15_64[country])+ int(males65[country]))
            male_total=(males0_14[country]+ males15_64[country]+ males65[country]) #male total from all ages
            female_total=(females0_14[country]+ females15_64[country]+ females65[country]) #female total from all ages
            both0_14=(int(males0_14[country])+ int(females0_14[country])) #total of both sexes from 1st file
            both15_64=(int(males15_64[country])+ int(females15_64[country])) #total of both sexes from 2nd file
            both65=(int(males65[country])+ int(females65[country])) #total of both sexes from 3rd file
            print(format("Country","20s")+ format("Age","20s")+ format("Both Sexes","20s")+ format("Male","20s")+ format("Female","20s")+ format("%Both","20s")+ format("%Male","20s")+ format("%Female","20s")+ format("Male to Female Ratio","20s")) #headers
                                    #name                   age                     both sexes             males                              females                                  %both
            print(format(countries[country], "20s")+ format("Total","20s")+ format(bothSexes_total, "20d")+ format(male_total, "20d")+ format(female_total, "20d")+ format(100, "20d")+ format(100, "20d")+ format(100, "20d")+ format(male_total/ female_total) #total age groups                                                 %males                        %females                                                 ratio
            print(format(countries[country], "20s")+ format("0-14","20s")+ format(both0_14, "<20d")+ format(males0_14[country], "<20d")+ format(females0_14[country], "<20d")+ format(males0_14[country]+ females0_14[country])/totalSexes_both, "<20d")+ format((males0_14[country])/male_total, "<20d")+ format((females0_14[country])/female_total, "<20d")+ format((male0_14/female0_14)*100, "20d") #1st file
            print(format(countries[country], "20s")+ format("15-64","20s")+ format(both15_64, "<20d")+ format(males15_64[country], "<20d")+ format(females0_14[country], "<20d")+ format(males15_64[country]+ females15_64[country])/totalSexes_both, "<20d")+ format((males15_64[country])/male_total, "<20d")+ format((females15_64[country])/female_total, "<20d")+ format((male15_64/female15_64)*100, "20d") #2nd file
            print(format(countries[country], "20s")+ format("64+", "20s")+ format(both65, "<20d")+ format(males65[country], "<20d")+ format(females65[country], "<20d")+ format(males65[country]+ females65[country])/totalSexes_both, "<20d")+ format((males0_14[country])/male_total, "<20d")+ format((females0_14[country])/female_total, "<20d")+ format((male65/female65)*100, "20d") #3rd file

2 个答案:

答案 0 :(得分:2)

你忘了在前一行关闭你的父母。

答案 1 :(得分:0)

请记住,在python中,每当它返回错误并突出显示某个特定的单词/短语时,错误很可能在突出显示之前。所以说我有:

name = input("enter your name"

print(name)

然后突出显示print,然后错误就是之前的错误,因为你可以看到我忘了关闭输入函数的括号。

希望这会有所帮助

〜bobbeh