简体   繁体   English

ValueError: int() 以 10 为底的无效文字:'C',从字符串到 integer 的转换发生在哪里?

[英]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.

相关问题 ValueError:对于带有基数为10的int()的无效文字:'' - 字符串到int转换 - ValueError: invalid literal for int() with base 10: '' - string to int conversion 将字符串从 txt 文件转换为 integer 时,出现 ValueError: invalid literal for int() with base 10: - When converting string to integer from txt file, I get ValueError: invalid literal for int() with base 10: ValueError:int() 的无效文字,基数为 10:'string' - ValueError: invalid literal for int() with base 10: 'string' ValueError: int() 以 10 为底的无效文字:'c' - ValueError: invalid literal for int() with base 10: 'c' ValueError: 以 10 为基数的 int() 的无效文字:''将条目转换为整数 - ValueError: invalid literal for int() with base 10: ''turning entry into integer LeetCode 反向整数(ValueError:int() 的无效文字,基数为 10: '' ) - LeetCode Reverse Integer (ValueError: invalid literal for int() with base 10: '' ) ValueError: 以 10 为基数的 int() 的无效文字:''(串行端口通信字符串到整数错误) - ValueError: invalid literal for int() with base 10: '' (Serial port communication string to integer error) ValueError:int()以10为底的无效文字; 试图从浮点数中提取一个整数 - ValueError: invalid literal for int() with base 10; Trying to extract an integer from a float ValueError:int() 的无效文字,基数为 10:'SOME STRING' - ValueError: invalid literal for int() with base 10: 'SOME STRING' ValueError:int() 的无效文字,我的字符串以 10 为底 - ValueError: invalid literal for int() with base 10 for my string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM