Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/347.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.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 while循环关闭控制台_Python_Loops_Console - Fatal编程技术网

Python while循环关闭控制台

Python while循环关闭控制台,python,loops,console,Python,Loops,Console,每次我启动循环时,我的控制台都会关闭,我不明白为什么 index = "" while not index: index = int(input("Enter the index that you want: ")) 我认为您的问题是,您的循环只执行一次,然后退出(这是我根据您的代码可以想到的) 原因是:开始时,您的索引是”。因此notindex被评估为True,因为python将空字符串视为False。但在循环中,您正在为索引赋值。因此,在下一次运行中,notindex返回Fal

每次我启动循环时,我的控制台都会关闭,我不明白为什么

index = ""  
while not index:
    index = int(input("Enter the index that you want: "))

我认为您的问题是,您的循环只执行一次,然后退出(这是我根据您的代码可以想到的)

原因是:开始时,您的索引是
。因此
notindex
被评估为
True
,因为python将空字符串视为
False
。但在循环中,您正在为索引赋值。因此,在下一次运行中,
notindex
返回
False

以下是ti如何工作的示例:

>>> index = "" 
>>> not index
True  <--- True since string is empty
>>> index = 3
>>> not index  
False <--- False since string is having some value 
>index=“”
>>>非索引
真>>索引=3
>>>非索引
Falsea)您的代码似乎很好

$ python
Python 2.7.10 (default, Oct 14 2015, 16:09:02) 
[GCC 5.2.1 20151010] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> index=""
>>> while not index:
...     index=int(input("blabla: "))
... 
blabla: 2
>>> print(index)
2
或者使用python 3:

$ python3
Python 3.4.3+ (default, Oct 14 2015, 16:03:50) 
[GCC 5.2.1 20151010] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> index=""
>>> while not index:
...     index=int(input("bla: "))
... 
bla: 4
>>> print(index)
4

b) 我的控制台正在关闭:这似乎是实际问题。你能指定你的操作系统和终端吗?您是如何调用python解释器的?我猜你在窗户上?点击windowskey+r并键入“cmd”启动控制台,然后从那里运行python以查看输出。

如果有错误消息,您会收到哪条错误消息?包括完整的回溯。您的控制台关闭是什么意思?另外,编辑显示的代码,使其可以运行。现在缩进已关闭。我假设这是因为Python中的
计算结果为
False
,因此您从未进入循环。@MattCremeens:no,
index=”“
计算结果为False,因此
not index
为True,因此我们输入loop@smci你说得对。双重否定使我震惊