2013-07-24 5 views
1

아래 코드를 사용하여 아래와 같이 출력하고 있습니다. 제발 제안 해주세요.TypeError : 자이 썬에서 시퀀스 첨자는 정수 또는 슬라이스 오류 여야합니다.

def isServerRunning(nodename,servername): 
    """Returns a boolean to say if the server is running. 
    Not 100% accurate but should be close - relies on there only being an mbean for 
    a server if it's running""" 

    mbean = AdminControl.queryNames('type=Server,node=%s,name=%s,*' % (nodename,servername)) 
    if mbean: 
     return True 
    return False 

def chunks(l, n): 
    return [l[i:i+n] for i in range(0, len(l), n)] 


l = ['acsayul33450Node00', 'dmgr', 'acsayul33450Node01', 'gndtaskprd1f1', 'acsayul33450Node01', 'nodeagent', 'acsayul33451Node01', 'gndtaskprd1f2', 'acsayul33451Node01', 'nodeagent'] 
jvmList1 = chunks(l, 2) 
sNameList = [] 
jvmList2 = [] 
N = 0 
S = 0 
for jvms in jvmList1: 
    N += 1 
    S = str(N) 
    jvm = (jvms[1]) 
    node = (jvms[0]) 
    status = "stopped" 
    jvmList2 = jvm 
    if isServerRunning(node,jvm): 
     status = "running" 
     print("\n") 
    sNameList.append(" %2s. %s %s %s" % (S, jvm, node, status)) 
X = 1 
while X: 
     print("\n".join(sNameList)) 
     choice = raw_input("\nEnter your choice [1-%s] : " % (S)) 

     try: 
      ServerName = jvmList2[choice] 
      X = 0 
     except KeyError: 
       print("\nInvalid entry\n") 
print ServerName 

출력은 다음과 같습니다. 귀하의 choice

wsadmin>execfile('C:\IBM\pyscripts\test\10.py') 


    1. dmgr acsayul33450Node00 running 
    2. gndtaskprd1f1 acsayul33450Node01 stopped 
    3. nodeagent acsayul33450Node01 stopped 
    4. gndtaskprd1f2 acsayul33451Node01 stopped 
    5. nodeagent acsayul33451Node01 stopped 

Enter your choice [1-5] : 1 
WASX7015E: Exception running command: "execfile('C:/IBM/pyscripts/test/10.py')"; 
exception information: 
com.ibm.bsf.BSFException: exception from Jython: 
Traceback (innermost last): 
    File "<input>", line 1, in ? 
    File "C:/IBM/pyscripts/test/10.py", line 38, in ? 
TypeError: sequence subscript must be integer or slice 

답변

1
ServerName = jvmList2[choice] 

은 문자열입니다. 먼저 int으로 변환해야합니다.

choice = int(raw_input("\nEnter your choice [1-%s] : " % (S))) 
+0

안녕하세요. 고마워요, 작동합니다. 나는 파이썬을 배우기 때문에 기초를 모른다. 고맙습니다. –

+0

실제로 10 분 전에 받아 들일 수 있도록 허용하지 않습니다. 이제 내가 그랬다. 고맙습니다. –

관련 문제