【问题标题】:PyCharm problem with console emulator - cannot type into emulated console控制台模拟器的 PyCharm 问题 - 无法输入模拟控制台
【发布时间】:2020-05-19 07:31:48
【问题描述】:

我最近在使用此构建信息时遇到了 PyCharm 问题:

模拟器的问题是在使用用户输入时无法在模拟器控制台中输入内容。当您关闭控制台仿真并在 python 控制台中运行您的脚本时,python 脚本本身可以工作。但是通过模拟器运行对我和其他人来说会更舒服。您收到的错误是这个:

Zadej pozici:回溯(最近一次通话最后一次):
文件“C:/Users/Adam/Desktop/Engeto/python/STUFF/1D_piskvorky/1D_pisk.py”,第 79 行,在
piskvorky_1d()
文件“C:/Users/Adam/Desktop/Engeto/python/STUFF/1D_piskvorky/1D_pisk.py”,第 70 行,在 piskvorky_1d
hrac_vstup= int(input("Zadej pozici:"))
ValueError: int() 以 10 为底的无效文字:''

这意味着 python 收到了一个空值 int() 我想,但是当你尝试输入一些字符而不是数字时:

ValueError: int() 以 10 为底的无效文字:'ssss'

模拟器会看到您输入的是字符而不是数字。如果有人遇到这个问题,请告诉我,什么是适合您的解决方案,也许这可以帮助其他人找到解决方案。

【问题讨论】:

    标签: python pycharm ide


    【解决方案1】:

    您声称无法在模拟控制台中输入内容是不正确的。您输入的字符串不是 input 的有效参数。错误消息是正确的。与直接在交互式python中输入相同。

    Python 3.9.0a3+ (heads/master:46874c26ee, Jan 30 2020, 20:04:52) [MSC v.1900 32
    bit (Intel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> s = int(input('Enter number: '))
    Enter number:
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ValueError: invalid literal for int() with base 10: ''
    >>> s = int(input('Enter number: '))
    Enter number: xxxx
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ValueError: invalid literal for int() with base 10: 'xxxx'
    

    它们也与从文件中运行相同代码时相同。 input 返回一个字符串。重新阅读 int 文档中关于字符串参数的部分。

    If x is not a number or if base is given, then x must be a string,
    bytes, or bytearray instance representing an integer literal in the
    given base.  The literal can be preceded by '+' or '-' and be surrounded
    by whitespace.  The base defaults to 10.  Valid bases are 0 and 2-36.
    Base 0 means to interpret the base from the string as an integer literal.
    >>> int('0b100', base=0)
    4
    

    '' 和 'ssss' 都不代表以 10 为底的字符串字面量。

    【讨论】:

    • 错误信息没问题,我知道我不能将字符串视为 int 但这不是问题。问题是无法将数字写到控制台中。您会看到,当我实际键入数字时,它们不会出现在控制台本身中,并且输入在进一步执行中被视为空字符串。但是当我尝试键入像“sss”这样的字符时(只是为了看看我是否能够写入控制台)它们仍然不会出现在模拟控制台中,但是它们的输入不会被视为空,你会在进一步执行中看到这个错误- 这没关系,因为您将字符串输入 int。
    • 好的。我的经验仅适用于 IDLE,而不是 Pycharm,所以我无法理解您现在解释的“不会出现”。我会尝试分离调用并添加打印 - s = input('# ')print(s)h = int(s) 以确保输入返回为空。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-25
    • 1970-01-01
    • 1970-01-01
    • 2022-11-29
    相关资源
    最近更新 更多