Я начинающий при кодировании в 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