【问题标题】:Python3 integer input gives ValueError?Python3整数输入给出ValueError?
【发布时间】:2018-08-25 18:17:11
【问题描述】:
width = input("Please enter grid width:")
height = input("Please enter grid height:")
grid = [["o" for x in range(width)] for y in range(height)]

在网格线上出现此错误:TypeError: 'str' object cannot be mapped as an integer

所以也许将输入设为整数?

width = int(input("Please enter grid width:"))

宽度线给了我:ValueError: invalid literal for int() with base 10: '{"command":"eval","data":"15","error":""}'

当它要求输入时,我输入了 15,这是一个数字。所以基本上输入想要是一个字符串,当我尝试将它转换为整数时它不会。

【问题讨论】:

  • 无法重现:使用int(input(...)) 确实解决了错误。
  • 尝试将您的输入转换为 int(input("请输入 ...")
  • 你确定你使用的是 Python 3.x 吗?如果您使用 Python 2.x,则应尝试将 int(input()) 替换为 int(raw_input())

标签: python python-3.x input integer valueerror


【解决方案1】:

只需在输入函数后更改类型即可:

width = input("Please enter grid width:")
width = int(width)
height = input("Please enter grid height:")
height = int(height)
grid = [["o" for x in range(width)] for y in range(height)]
print (grid)

【讨论】:

  • 那么我得到的错误是: ValueError: invalid literal for int() with base 10: '{"command":"eval","data":"5","error":" "}' 知道发生了什么...
【解决方案2】:

在我看来,您在提示时输入了小数 - 请记住,int 只能使用整数值。因此出现“invalid literal for int() with base 10”错误消息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-31
    • 2021-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-04
    • 2015-05-18
    • 2021-07-20
    相关资源
    最近更新 更多