Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/361.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-True-miltiple时间_Python - Fatal编程技术网

Python 使用while-True-miltiple时间

Python 使用while-True-miltiple时间,python,Python,我是新来的,这是我的第一个问题。为任何缺点道歉 我正在尝试使用简单的代码进行练习。此示例具有以下输出: *who are you? Joe Hello Joe. What is the password? (It is a fish) hoki Access granted Hello Joe. What is the password? (It is a fish)* 我使用的代码是: 为True时: 打印('你是谁?') 名称=输入() 如果名称!='乔: 持续 尽

我是新来的,这是我的第一个问题。为任何缺点道歉

我正在尝试使用简单的代码进行练习。此示例具有以下输出:

*who are you?  
Joe  
Hello Joe. What is the password? (It is a fish)  
hoki  
Access granted  
Hello Joe. What is the password? (It is a fish)*  
我使用的代码是:

为True时:
打印('你是谁?')
名称=输入()
如果名称!='乔:
持续
尽管如此:
打印(‘你好,乔,密码是什么?(它是一条鱼)’)
密码=输入()
如果是密码!='hoki':
持续
打印('已授予访问权限')*
当我试图使用continue语句退出第二个
而True
循环时(如果密码问题的答案是
hoki
),它再次问
“你好,乔,密码是什么?(它是一条鱼)*
它应该是结束,不再问我的密码,因为我已经输入了正确的密码

任何建议都将不胜感激。 谢谢

这是解决方案

user_check = True
while user_check:
    print('who are you?')
    name = input()
    if name != 'Joe':
        continue
    while True:
        print('Hello Joe. What is the password? (It is a fish)')
        password = input()
        if password !='hoki':
            continue
        else:
            print('Access granted')
            user_check = False
            break

您可以在代码中使用break语句来结束循环

while True:
    print('This loop will stop because of my break statement!')
    break

print('This will print out because we used the break statement!')

代码的问题在于它只是在使用
continue
。程序并没有语句可以在循环
时中断

您只需要两个
break
语句,如下所示:

while True:
    print('who are you?')
    name = input()
    if name != 'Joe':
        continue
    while True:
        print('Hello Joe. What is the password? (It is a fish)')
        password = input()
        if password !='hoki':
            continue
        print('Access granted')
        break # break inner loop
    break # break outer loop

就像其他人说的那样,你应该使用
break
而不是
continue
,但是你还需要将密码检查更改为'password=='hoki'

  • 删除第一条
    语句之前的
    *
    ,同时删除
    语句,然后删除
    打印语句之后的
    (“已授予访问权限”
  • 缩进后面的所有代码,使其在
    while
    循环中执行
  • 最后,按照以下步骤添加中断语句:
  • 为True时:
    打印('你是谁?')
    名称=输入()
    如果名称!=“Joe”:
    持续
    尽管如此:
    打印(‘你好,乔,密码是什么?(它是一条鱼)’)
    密码=输入()
    如果密码!='hoki':
    持续
    打印('已授予访问权限')
    打破
    打破
    
    这应该会产生预期的结果。解决问题的更“语义”的方法可能是:

    logged_in=False
    未登录时:
    打印('你是谁?')
    名称=输入()
    如果name=='Joe':
    未登录时:
    打印(‘你好,乔,密码是什么?(它是一条鱼)’)
    密码=输入()
    如果密码=='hoki':
    打印('已授予访问权限')
    已登录=真
    #代码仅可供登录用户访问
    打印(“现在让我们做一些只有乔才能做的事情…”)
    

    希望这有帮助!

    不要直接使用While True;将其设为While(变量)。然后继续您的逻辑。请参见下面的示例:

    a = True
    
    while a:
    
         # apply logic
         # if you want to break this loop; assign False to a.
    
         b = True
    
         while b:
    
             # apply logic
             # if you want to break this loop; assign False to b
    
    
             # .... and so on
    

    希望这有帮助。

    使用break来终止循环是的,我实际上使用了一本书中使用break的例子,我认为我应该继续。是的,确实如此。这个答案和其他建议使用两个缺失的break的答案有助于我的理解。是的,perfect错过了中断来关闭while循环。