-2
내 GCSE 개정판의 주제에서 모든 질문을 무작위로 생성하는 프로그램을 만들려고합니다. 내 코드는 임의의 질문 후에 오류 메시지를 생성합니다.Python - IndexError : 튜플 인덱스가 범위를 벗어났습니다.
computing_UNIT4_networks = (["LOCAL AREA NETWORK","A network in one geographical area."],["WIDE AREA NETWORK","A network spanning two or more geographical areas."],["ROUTER","Connects a network to another network, sends and inspects packets of data."],["SWITCH","Channels data to its indented destination, within an internal network."],["NETWORK INTERFACE CARD","A card that allows a computer to connect to the network."],["FIBRE OPTIC CABLE","Uses light to transmit data."],["ETHERNET CABLE","Uses metal wires (usually copper) to transmit data."],["DNS","Used to match IP addresses to URL."],["HOSTING","Storing data for a user, usually a website."],["CLOUD COMPUTING","A remote computer is used to store data and provide services."])
computing_UNIT5_protocols = (["STAR NETWORK","All computers are connected 'individually' to the server.(using switches)"],["FULL MESH NETWORK","Every computer is connected to every other."],["ENCRYPTION","Disguising data so that it can be read with the key."],["DATA PACKET","Small unit of data to be transmitted."],["LATENCY","The delay in receiving data."],["BANDWIDTH","The amount of data that can be transmitted in a set amount of time."],["PACKET SWITCHING","Packets of data are transmitted and are able to take individual routes to their destination."],["CIRCUIT SWITCHING","Packets of data are transmitted along the same route to their destination."])
done = []
def computing():
unit = int(input("Which UNIT are you revising?\n-"))
if unit == 4:
UNIT = computing_UNIT4_networks
print("Here are",len(UNIT),"questions on UNIT4 - Wired and wireless networks.")
elif unit == 5:
UNIT = computing_UNIT5_protocols
print("Here are",len(UNIT),"questions on UNIT5 - Network topologies, protocols and layers.")
i=1
c=0
while i <= (len(UNIT)):
import random
randint=random.randint(0,len(UNIT))
while randint in done:
randint=random.randint(0,len(UNIT))
if randint in done:
i=i
else: break
question = UNIT[randint][1]
answer = UNIT[randint][0]
print("\nWhat is this the definition of?:",question)
b=input("-").upper()
if b == answer:
c=c+1
print("\nCorrect\nCurrent score:",c,"/",i)
else: print("\nWrong. The answer was",answer,".\nCurrent score:",c,"/",i)
done.append(randint)
i = i+1
print("\nYou scored",c,"/",len(UNIT),".")
computing()
오류 메시지 :
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
computing()
File "[FILELOCATION]", line 22, in computing
question = UNIT[randint][1]
IndexError: tuple index out of range
'randint'는 끝점이므로'len (UNIT)'를 반환 할 수 있습니다. 일반적으로 대신 ['randrange'] (https://docs.python.org/2/library/random.html#random.randrange)를 사용하는 것이 좋습니다. – khelwood
질문을 제출하기 전에 코드를 철저히 테스트해야합니다. SO를 첫 번째 지원 라인으로 사용하지 마십시오. 그렇게하면 배우지 못할 것입니다. – Olian04
[PYTHON IndexError : 튜플 인덱스가 범위를 벗어났습니다] 가능한 복제본 (http://stackoverflow.com/questions/24167373/python-indexerror-tuple-index-out-of-range) –