[英]ValueError: invalid literal for int() with base 10: 'C' , Where is the conversion from string into integer happening?
I have reviewed similar errors .我已经审查了类似的错误。 It seems that somewhere in my code I am converting a string into integer.
似乎在我的代码中某处我将字符串转换为 integer。 But I do not know where.
但我不知道在哪里。
I have an input list 'ops', which contains strings.我有一个输入列表'ops',其中包含字符串。 I also have another list 'record' that I am filling up with integers.
我还有另一个列表“记录”,我用整数填充。
def baseball_game (ops):
record = []
for i in range (len(ops)):
if ops[i] == 'C':
record.pop() #I remove last element. I am mot converting into integer.
print(record)
if ops[i] == 'D':
record.append(record[-1] * 2) #I am doubling the value of last element, which is already an integer.
print(record)
if ops[i] == '+':
record.append(record[-1] + record[-2]) #I am adding last 2 elements, which are NOT integers.
print(record)
else:
record.append(int(ops[i])) #I am converting into integer any value which is NOT 'C', 'D' nor '+'
print(record)
return sum(record)
The problem is that you have 3 if
blocks in a row, and you are expecting them to behave like elif
s.问题是您连续有 3 个
if
块,并且您希望它们的行为类似于elif
。
When ops[i]
is 'C'
, it enters the first if
block, and then, because 'C' != '+'
, it enters the else
block at the end.当
ops[i]
为'C'
时,它进入第一个if
块,然后,因为'C' != '+'
,它在最后进入else
块。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.