목록에서 항목을 인쇄 할 때 문제가 있습니다.목록에 추가하는 입력이 있습니다. 마지막 목록 항목 (마지막 입력)을 어떻게 인쇄합니까?
countryList = []
cityList = []
def startFunction():
while True:
print("\nWhen you have typed in country and city, press 3 in the menu to see the weather forecast for your choice.\n")
menu = input("\nPress 1 for country\nPress 2 for city\nPress 3 to see forecast\nPress 4 to exit\n")
if menu == "1":
countryFunction()
elif menu == "2":
cityFunction()
elif menu == "3":
forecastFunction()
else:
print ("\nGoodbye")
break
내가 먼저 다른 함수를 호출하는 루프 국가 및 도시와 다음 startfunction에 대해 빈 목록을 가지고 :
다음은 관련 코드입니다.
여기에 국가를 선택하는 기능과 같은 방법에있어 :
def countryFunction():
countryc = input("Enter which country your city is in(in english): ")
countryList.append(countryc)
그리고 인쇄 기능은 다음과 같습니다 : 당신은 그냥했습니다 순간에 볼 수있는
def forecastFunction():
r = requests.get("http://api.wunderground.com/api/0def10027afaebb7/forecast/q/" + countryList[0] + "/" + cityList[0] + ".json")
data = r.json()
#Above is the problem, countryList[0] and cityList[0]
countryList[0]
이라고 입력하면 목록의 첫 번째 항목 만 인쇄됩니다. 그리고 내가 사용하고있는 루프 때문에 사용자는 매번 목록에 추가 할 국가와 도시를 반복해서 선택할 수 있습니다. 내 질문은 : 나는
r = requests.get("http://api.wunderground.com/api/0def10027afaebb7/forecast/q/" + countryList[0] + "/" + cityList[0] + ".json"
[목록의 마지막 요소를 얻는 방법] 가능한 복제본 (영문) (http://stackoverflow.com/questions/930397/how-to-get-the-last-element-of-a-list) – darthbith