阻力计算器说索引超出范围

时间:2017-10-28 11:36:02

标签: python python-3.x

我正在尝试编写一个程序,收集所有电阻,计算,然后获得电压,然后计算电流。以下是我到目前为止的情况:

resistors = [0]


def ObtainResistors(resistors):
    for i in range(1, 8):
            value = int(input('please enter resistor %d:' % i))
            resistors.append(value)
            return


def TotalResistance(resistors):
    rt1 = ((resistors[2] * resistors[3]) / (resistors[2] + resistors[3]))
    print(rt1, 'ohms')
    rt2 = (rt1 + resistors[4])
    print(rt2, 'ohms')
    rt3 = ((rt2 * resistors[5]) / (rt2 + resistors[5]))
    print(rt3, 'ohms')
    rt4 = (rt3 + resistors[7])
    print(rt4, 'ohms')
    rt5 = ((rt4 * resistors[6]) / (rt4 + resistors[6]))
    print(rt5, 'ohms')
    rt6 = (rt5 + resistors[1])
    print(rt6, 'ohms')
    rt = (rt1 + rt2 + rt3 + rt4 + rt5 + rt6)
    print(rt,  'ohms')
    return


ObtainResistors(resistors)
print(resistors)
TotalResistance(resistors)
print(resistors)

这是我在运行时得到的

please enter resistor 1:1
Traceback (most recent call last):
  File "C:/Users/User/PycharmProjects/Introduction_to_Python/RESISTOR COURSEWORK.py", line 31, in <module>
    TotalResistance(resistors)
  File "C:/Users/User/PycharmProjects/Introduction_to_Python/RESISTOR COURSEWORK.py", line 12, in TotalResistance
    rt1 = ((resistors[2] * resistors[3]) / (resistors[2] + resistors[3]))
IndexError: list index out of range
[0, 1]

Process finished with exit code 1

1 个答案:

答案 0 :(得分:0)

如果循环<php> <env name="APP_ENV" value="testing"/> <env name="DB_CONNECTION" value="sqlite"/> <env name="DB_DATABASE" value=":memory:"/> <env name="CACHE_DRIVER" value="array"/> <env name="SESSION_DRIVER" value="array"/> <env name="QUEUE_DRIVER" value="sync"/> </php> ,则只读取1个电阻值。

return移出循环,或删除它,因为Python不需要显式返回并且您没有返回值:

return