2013-02-27 4 views
1

목표는 코드의 마지막 인쇄 행이지만 실제로이 오류가 계속 발생하기 때문에이 오류가 계속 발생합니다. TypeError: 'int'및 'str'에 대해 지원되지 않는 피연산자 유형이 지원됩니다. 가능한 한 출력 부품 만 빠르게 변경할 수 있습니까? 나는 여전히 그것들을 처음부터 int로 변환 할 필요가있다.하지만이 출력물의 경우에는 int 옆에 "Population"과 "Area"라는 단어가 필요하다!동일한 출력 줄에 여러 유형이 있습니다.

또는 인수로 그들에 전달하고 그것의 print 돌봐 보자 :

def _demo_fileopenbox():   
    msg = "Pick A File!" 
    msg2 = "Select a country to learn more about!" 
    title = "Open files" 
    default="*.py" 
    f = fileopenbox(msg,title,default=default) 
    writeln("You chose to open file: %s" % f)  
    countries = {} 

     with open(f,'r') as handle: 

     reader = csv.reader(handle, delimiter = '\t') 

     for row in reader: 

     countries[row[0]] = int(row[1].replace(',', '')), int(row[2].replace(',', '')) 

     reply = choicebox(msg=msg2, choices= list(countries.keys())) 

     print(reply) 

     print((countries[reply])[0]) 

     print((countries[reply])[1]) 

     #print(reply + "- \tArea: + " + (countries[reply])[0] + "\tPopulation: " + (countries[reply])[1]) 

답변

3

당신은 str()으로 문자열로 변환해야 할 것이에

print(reply + "- \tArea:", countries[reply][0] + "\tPopulation:", countries[reply][1]) 

비록를 요점은 문자열 형식을 사용하는 것입니다 :

print('{}- \tArea: {}\tPopulation: {}'.format(reply, rountries[reply][0], rountries[reply][1])) 
그러나 %의 대신에 :

print(reply + "- \tArea: %s" % countries[reply][0] + "\tPopulation: %s" + % countries[reply][1]) 

Python3하여 사용하는 것이 좋습니다 {의}

+0

네, 제가 게시 직후에 그걸 알아 들었습니다. output = reply + "- \ tArea :"+ str ((countries [reply]) [0]) + "\ tPopulation :"답장 = 선택 상자 (메시지 = msg2, 선택 = 목록 (countries.keys) + str (국가 [reply]) [] print (output)'답장을 보내 주셔서 감사합니다! – erp

+0

@erp :'(foo) [0]'을 할 필요가 없습니다. 괄호없이'foo [0]'이라고 써주십시오. – Blender

1

은 또는 당신은 당신이 문자열을 사용하는 인쇄 라인을 이야기하는 '%'기호를 사용할 수 있습니다. 처음에는 힘들어 보일지 모르지만 그렇게 나쁘지는 않으며 유용 할 수 있습니다.

print("{reply}-\tArea: {area}\tPopulation: {population}".format(reply=reply,area=countries[reply][0],population=countries[reply][1])) 
관련 문제