我必须使用for循环来收集用户输入值的数据:
Number of electrodes: 5
Current in Electrode #1: 10
Current in Electrode #2: 211
Current in Electrode #3: 350
Current in Electrode #4: 410
Current in Electrode #5: 220
并使用for循环(必须是for循环)创建输入提示,如上面所示
目前我的代码是这样的:
cap=int(input("Number of capacitors: "))
for num in range(cap):
val=num+1
input("Current in Electrode#",val)
但是我遇到了问题,因为你不能在输入语句中有一个值,这样显然…?我基本上不知道如何在for循环创建的输入语句中遍历数字。请帮助!
下面的代码应该可以为您工作:
cap = int(input("Number of electrodes: "))
info = []
for i in range(1, cap+1):
current = int(input("Current in Electrode #"+str(i)+": "))
info.append(current)
print info
Number of electrodes: 5
Current in Electrode #1: 45
Current in Electrode #2: 12
Current in Electrode #3: 786
Current in Electrode #4: 120
Current in Electrode #5: 210
[45, 12, 786, 120, 210]
使用for
循环来获取您想要的每个数字,然后转换为字符串,以便我们可以进行字符串连接。您的问题可能是您没有首先转换为字符串