Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何获取空输入?_Python_Python 3.x - Fatal编程技术网

Python 如何获取空输入?

Python 如何获取空输入?,python,python-3.x,Python,Python 3.x,我希望用户只向所需的变量输入值(例如:如果需要获得幂,则忽略输入幂,只向其余变量输入)。我真的很感激你的帮助 代码: 错误: Traceback (most recent call last): File "C:\Users\tharu\Desktop\Python.py", line 1, in <module> Power = int(input("Power: ")) ValueError: invalid literal f

我希望用户只向所需的变量输入值(例如:如果需要获得幂,则忽略输入幂,只向其余变量输入)。我真的很感激你的帮助

代码:

错误:

Traceback (most recent call last):
  File "C:\Users\tharu\Desktop\Python.py", line 1, in <module>
    Power = int(input("Power: "))
ValueError: invalid literal for int() with base 10: ''
回溯(最近一次呼叫最后一次):
文件“C:\Users\tharu\Desktop\Python.py”,第1行,在
功率=整数(输入(“功率:”)
ValueError:基数为10的int()的文本无效:“”

您收到此错误是因为您将输入留空。您需要将整数值作为输入,否则您可以调节输入函数或使用命令行参数。

不要立即将其转换为
int
,并检查输入的真实性

Power=输入(“Power:”)
电流=输入(“电流:”)
电压=输入(“电压:”)
def P(C,V):
返回C*V
def C(P,V):
返回P/V
def V(P,C):
返回P/C
如果不是电源:
打印(P(int(电流)、int(电压)))
elif非当前版本:
打印(C(内部(电源)、内部(电压)))
其他:
打印(V(整数(电源)、整数(电流)))
>>>权力:
>>>学生:2
>>>电压:20
40

空字符串将报告
False
,因此您可以使用该事实来知道哪个输入留空。一旦您知道了这一点,您就可以将其他字符串转换为该部分代码中的
int

在尝试将其转换为整数之前,您是否尝试使用
if
语句检查字符串是否为空?这是否回答了您的问题?不要在布尔上下文中使用
bool
,它是无用的。只需执行
if-mystring:
来测试它是否为空,或者执行
if-not-mystring:
来测试它是否为空。OP希望能够输入一个空字符串,表示他希望从其他字符串计算值。
Traceback (most recent call last):
  File "C:\Users\tharu\Desktop\Python.py", line 1, in <module>
    Power = int(input("Power: "))
ValueError: invalid literal for int() with base 10: ''