You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PyCharm运行Python输入代码报错,第二个输入为空(VS运行正常)

问题描述

我写了以下代码:

def area(x, y):
    return x*y
w = int(input())
h = int(input())
print(area(w,h))

在PyCharm中运行时总是报错(VS中运行正常),错误信息如下:

ValueError: invalid literal for int() with base 10: ''

换成float()后依然报错:

ValueError: could not convert string to float: ''

看起来第二个输入始终是空值。

环境信息:Python 3.8,PyCharm 2022.1.1(社区版),Build #PC-221.5591.52,构建于2022年5月10日,运行时版本:11.0.14.1+1-b2043.45 amd64

解决办法

这是PyCharm运行配置的输入设置问题,按以下步骤调整:

  • 点击PyCharm右上角运行配置下拉框,选择Edit Configurations
  • 在打开的窗口中找到当前脚本的运行配置,切换到Execution标签页
  • 若需要运行时手动输入内容,勾选Run with Python console选项;若想提前预设输入值,在Program arguments中填入用空格分隔的两个数值(比如4 6)
  • 保存配置后重新运行脚本

另外,也可以给代码添加输入提示,避免输入时出现混淆:

def area(x, y):
    return x*y
w = int(input("请输入宽度:"))
h = int(input("请输入高度:"))
print(area(w,h))

内容的提问来源于stack exchange,提问作者snpay

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 11:15:37