我是Python編程的初學者。我正在編寫這樣的代碼,它應該讓用戶在幾張表格中看到世界上的人口信息,然後有幾個選項只是最高和最低男女比例的句子等。語法錯誤 - 突出顯示一個變量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
更具體地說,'國家=(countries.index(country_input)'有2個開放的括號和一個密切括號。 – Amber 2014-12-07 21:01:00