为什么我会收到SyntaxError?

2024-06-01 06:57:28 发布

您现在位置:Python中文网/ 问答频道 /正文

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

The following program uses a list to store a user-entered set of resistance values and computes I. Modify the program to compute the voltage drop across each resistor, store each in another list voltage_drop, and finally print the results in the specified format.

以下是未经我修改的代码:

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行

SyntaxError:分析时出现意外的EOF


Tags: thetoinformatinputrescurrentprogram