为什么我会收到 SyntaxError?

时间:2021-02-18 10:27:06

标签: python python-3.x syntax-error

我正在处理的问题是要求我修改现有代码如下:

<块引用>

以下程序使用列表来存储用户输入的一组电阻值并计算 I。修改程序以计算每个电阻上的电压降,将每个存储在另一个列表中电压降,最后将结果打印在指定的格式。

这是未经我修改的代码:

resistors = []
voltage_drop = []

print( '%d resistors are in series.' % num_resistors)
print('This program calculates the'),
print('voltage drop across each resistor.')

input_voltage = float(input('Input voltage applied to circuit: '))
print (input_voltage)

print('Input ohms of {} resistors'.format(num_resistors))
for i in range(num_resistors):
    res = float(input('{})'.format(i + 1)))
    print(res)
    resistors.append(res)

#  Calculate current
current = input_voltage / sum(resistors)

# Calculate voltage drop over each resistor
# ...

# Print the voltage drop per resistor
# ...


这是我修改后的代码:

resistors = []
voltage_drop = []

print( '%d resistors are in series.' % num_resistors)
print('This program calculates the'),
print('voltage drop across each resistor.')

input_voltage = float(input('Input voltage applied to circuit: '))
print (input_voltage)

print('Input ohms of {} resistors'.format(num_resistors))
for i in range(num_resistors):
    res = float(input('{})'.format(i + 1)))
    print(res)
    resistors.append(res)

#  Calculate current
current = input_voltage / sum(resistors)

# Calculate voltage drop over each resistor
for res in resistors:
    vol_drop = current * res
    voltage_drop.append(vol_drop)

# Print the voltage drop per resistor

print('Voltage drop per resistor is')
for voldrop in voltage_drop:
    print('{}) {:.2f} V'.format(voltage_drop.index(voldrop), voldrop)

如果我注释掉最后三行或将其更改为仅打印电压降,则效果很好。它应该像

一样打印
  1. 第一个数字
  2. 第二个数字 等等。

这是我收到的错误:

文件“main.py”,第 31 行

语法错误:解析时出现意外 EOF

1 个答案:

答案 0 :(得分:0)

打印语句缺少结束')':

 print('{}) {:.2f} V'.format(voltage_drop.index(voldrop), voldrop)