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 为什么';我的代码不能进入循环吗?_Python_Loops - Fatal编程技术网

Python 为什么';我的代码不能进入循环吗?

Python 为什么';我的代码不能进入循环吗?,python,loops,Python,Loops,我看不出有什么问题。 为什么它不进入一个循环 不打印任何内容,只是卡住了。此处不需要flag=False: def is_number(s): try: float(s) return True except ValueError: return False flag = True while flag != False: numInput = raw_input("Enter your first number:

我看不出有什么问题。
为什么它不进入一个循环

不打印任何内容,只是卡住了。

此处不需要flag=False:

def is_number(s):
    try:
        float(s)
        return True
    except ValueError:
        return False  


flag = True
while flag != False:
    numInput = raw_input("Enter your first number: ")
    if is_number(numInput):
        numInput = float(numInput)
        flag = True
        break
    else:
        print "Error, only numbers are allowed"
演示:

试试这个:

Enter your first number: foo
Error, only numbers are allowed
Enter your first number: bar
Error, only numbers are allowed
Enter your first number: 123

flag!=False
与flag相同。同样地,
is_number(numInput)=True
应该是
is_number(numInput)
。您可能想告诉我们您的代码应该做什么。这是计算器的第一部分。如果您第一次没有看到“输入您的第一个数字”,请,那么您的缩进可能有问题。尝试在while循环之前打印一些内容,看看它在哪里卡住了,或者如果您在标准输出上看不到任何东西(这是原始输入应该打印“输入您的第一个…”文本的地方),同样在循环结束后,该值将被分配给
numInput
变量,以便您可以在脚本中使用它。。
while True:
    numInput = raw_input("Enter your first number: ")
    if is_number(numInput):
        numInput = float(numInput)
        break
    else:
        print "Error, only numbers are allowed"
Enter your first number: foo
Error, only numbers are allowed
Enter your first number: bar
Error, only numbers are allowed
Enter your first number: 123
while True:
    numInput = raw_input("Enter your first number: ")    
    try:
        numInput = float(numInput)
        break
    except:
        print "Error, only numbers are allowed"