Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 阻力计算器表示指数超出范围_Python_Python 3.x - Fatal编程技术网

Python 阻力计算器表示指数超出范围

Python 阻力计算器表示指数超出范围,python,python-3.x,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

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

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:1
回溯(最近一次呼叫最后一次):
文件“C:/Users/User/PycharmProjects/Introduction_to_Python/RESISTOR COURSEWORK.py”,第31行,在
总电阻(电阻器)
文件“C:/Users/User/PycharmProjects/Introduction_to_Python/RESISTOR COURSEWORK.py”,第12行,共电阻
rt1=((电阻器[2]*电阻器[3])/(电阻器[2]+电阻器[3]))
索引器:列表索引超出范围
[0, 1]
进程已完成,退出代码为1

如果在回路中返回
,则仅读取1个电阻值

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

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