python元组索引超出范围_Python-IndexError:元组索引超出范围

I am trying to create a program to randomly generate every question from a topic for my GCSE revision. My code generates an error message after a random question. Here is my code followed by the error message:

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()

Error Message:

Traceback (most recent call last):

File "", line 1, in

computing()

File "[FILELOCATION]", line 22, in computing

question = UNIT[randint][1]

IndexError: tuple index out of range

解决方案

randint should go from 0 to len(UNIT)-1. The value is probably randomly reaching the len(UNIT) and that index is out of bounds.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值