Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.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_While Loop_Break - Fatal编程技术网

Python中的简单while循环直到中断

Python中的简单while循环直到中断,python,while-loop,break,Python,While Loop,Break,一个非常简单的while循环语句是什么,它将继续下面的程序,直到用户键入“exit” 比如说, whileresponse=(!'exit') 继续文件 其他的 打破 打印(“谢谢,再见!”) #我知道这是完全错误的,但这是一次尝试! 我目前的档案: #/usr/bin/python 朋友={'John':{'phone':'0401', ‘生日’:‘7月31日’, “地址”:“英国”, “利益”:[a”,“b”,“c']}, “哈利”:{“电话”:“0402”, ‘生日’:‘8月2日’, '

一个非常简单的
while
循环语句是什么,它将继续下面的程序,直到用户键入“exit”

比如说,

whileresponse=(!'exit')
继续文件
其他的
打破
打印(“谢谢,再见!”)
#我知道这是完全错误的,但这是一次尝试!
我目前的档案:

#/usr/bin/python
朋友={'John':{'phone':'0401',
‘生日’:‘7月31日’,
“地址”:“英国”,
“利益”:[a”,“b”,“c']},
“哈利”:{“电话”:“0402”,
‘生日’:‘8月2日’,
'地址':'匈牙利',
“兴趣”:['d','e','f']}
response=raw_input(“请输入搜索条件,或键入“exit”退出程序:”).split()
尝试:
打印“%s的%s是%s%”(响应[0]、响应[1]、朋友[0]、[1]。]
除KeyError外:
打印“对不起,我不知道。请重试,或键入“退出”退出程序:”

您的while循环将继续,直到您设置的条件为false为止。所以你希望你的代码大部分都在这个循环中。完成后,您知道用户输入了“退出”,因此您可以打印错误消息

#!/usr/bin/python
friends = {'John' : {'phone' : '0401',
        'birthday' : '31 July',
        'address' : 'UK',
        'interests' : ['a', 'b', 'c']},
    'Harry' : {'phone' : '0402',
        'birthday' : '2 August',
        'address' : 'Hungary',
        'interests' : ['d', 'e', 'f']}}

response = ['']
error_message = "Sorry, I don't know about that. Please try again, or type 'exit' to leave the program: "

while response[0] != 'exit':
    response = raw_input("Please enter search criteria, or type 'exit' to exit the program: ").split()
    try:
        print "%s's %s is %s" % (response[0], response[1], friends[response[0]][response[1]])
    except KeyError:
        print error_message
    except IndexError:
        print error_message

print ('Thank you, good bye!')

这段代码是您想要的开始,但它仍然有一些bug。查看是否可以重新构造它,以便在用户输入“退出”时不会打印错误消息。

您的while循环将继续,直到您设置的条件为false为止。所以你希望你的代码大部分都在这个循环中。完成后,您知道用户输入了“退出”,因此您可以打印错误消息

#!/usr/bin/python
friends = {'John' : {'phone' : '0401',
        'birthday' : '31 July',
        'address' : 'UK',
        'interests' : ['a', 'b', 'c']},
    'Harry' : {'phone' : '0402',
        'birthday' : '2 August',
        'address' : 'Hungary',
        'interests' : ['d', 'e', 'f']}}

response = ['']
error_message = "Sorry, I don't know about that. Please try again, or type 'exit' to leave the program: "

while response[0] != 'exit':
    response = raw_input("Please enter search criteria, or type 'exit' to exit the program: ").split()
    try:
        print "%s's %s is %s" % (response[0], response[1], friends[response[0]][response[1]])
    except KeyError:
        print error_message
    except IndexError:
        print error_message

print ('Thank you, good bye!')

这段代码是您想要的开始,但它仍然有一些bug。查看是否可以对其进行重新构造,以便在用户输入“退出”时不会打印错误消息。

continue
是一个不需要参数的关键字。它只是告诉当前循环立即继续到下一个迭代。它可以在
内部使用,而
用于
循环

然后,您的代码应该放在
while
循环中,该循环将一直运行,直到满足条件为止。您的条件语法不正确。它应该在响应时读取
退出“:
。因为您使用的是条件,所以不需要
continue
语句。只要该值不是
“exit”
,它将按设计继续运行

然后,您的结构将如下所示:

response = ''
# this will loop until response is not "exit"
while response != 'exit':
    response = raw_input("foo")
如果要使用
continue
,则可能需要在响应上执行其他各种操作,并且可能需要提前停止并重试。
break
关键字是处理循环的类似方法,但它表示我们应该立即完全结束循环。您可能有一些其他条件是交易破坏者:

while response != 'exit':
    response = raw_input("foo")

    # make various checks on the response value
    # obviously "exit" is less than 10 chars, but these
    # are just arbitrary examples
    if len(response) < 10:
        print "Must be greater than 10 characters!"
        continue  # this will try again 

    # otherwise
    # do more stuff here
    if response.isdigit():
        print "I hate numbers! Unacceptable! You are done."
        break
while响应!='出口':
响应=原始输入(“foo”)
#对响应值进行各种检查
#显然,“退出”少于10个字符,但是
#这些只是武断的例子
如果len(响应)<10:
“打印”必须大于10个字符
继续#这将重试
#否则
#在这里做更多的事情
if response.isdigit():
打印“我讨厌数字!不可接受!你完了。”
打破

continue
是一个不需要参数的关键字。它只是告诉当前循环立即继续到下一个迭代。它可以在
内部使用,而
用于
循环

然后,您的代码应该放在
while
循环中,该循环将一直运行,直到满足条件为止。您的条件语法不正确。它应该在响应时读取
退出“:
。因为您使用的是条件,所以不需要
continue
语句。只要该值不是
“exit”
,它将按设计继续运行

然后,您的结构将如下所示:

response = ''
# this will loop until response is not "exit"
while response != 'exit':
    response = raw_input("foo")
如果要使用
continue
,则可能需要在响应上执行其他各种操作,并且可能需要提前停止并重试。
break
关键字是处理循环的类似方法,但它表示我们应该立即完全结束循环。您可能有一些其他条件是交易破坏者:

while response != 'exit':
    response = raw_input("foo")

    # make various checks on the response value
    # obviously "exit" is less than 10 chars, but these
    # are just arbitrary examples
    if len(response) < 10:
        print "Must be greater than 10 characters!"
        continue  # this will try again 

    # otherwise
    # do more stuff here
    if response.isdigit():
        print "I hate numbers! Unacceptable! You are done."
        break
while响应!='出口':
响应=原始输入(“foo”)
#对响应值进行各种检查
#显然,“退出”少于10个字符,但是
#这些只是武断的例子
如果len(响应)<10:
“打印”必须大于10个字符
继续#这将重试
#否则
#在这里做更多的事情
if response.isdigit():
打印“我讨厌数字!不可接受!你完了。”
打破

即,我的主要问题是是否有表示“继续运行文件”的特定语句,即我的主要问题是是否有表示“继续运行文件”的特定语句