坚持自动化无聊的东西第2章-继续声明

问题描述

我目前在《自动完成无聊的事情》的第2章,并停留在Continue语句上。给出的示例是,如果输入的名称是“ Joe”,密码是“ swordfish”,则程序将显示“ Access access”。但是,我不确定为什么我会继续打印“你是谁?”当name ='Joe'和password ='swordfish'的条件已经满足时。谁能告诉我为什么我仍然停留在while循环中?

spec:
  replicas: 1
  strategy: 
    type: Recreate
  template:
    Metadata:
      creationTimestamp: null
      labels:
        io.kompose.service: server
    spec:
      imagePullSecrets:
        - name: <secret_name>

解决方法

询问密码时,您使用的是{% extends "base.html" %} {% block title %}Log-in{% endblock %} {% block content %} <h1>Log-in</h1> {% if form.errors %} <p> Your username and password didn't match. Please try again. </p> {% else %} <p>Please,use the following form to log-in. If you don't have an account <a href="{% url "register" %}">register here</a></p> {% endif %} <div class="login-form"> <form action="{% url 'login' %}" method="post"> {{ form.as_p }} {% csrf_token %} <input type="hidden" name="next" value="{{ next }}" /> <p><input type="submit" value="Log-in"></p> </form> <p><a href="{% url "password_reset" %}">Forgotten your password?</a></p> </div> <div class="social"> <ul> <li class="facebook"> <a href="{% url "social:begin" "facebook" %}">Sign in with Facebook</a> </li> <li class="twitter"> <a href="#">Login with Twitter</a> </li> <li class="google"> <a href="#">Login with Google</a> </li> </ul> </div> {% endblock %} ,而不是print。另外请注意,您正在将变量inputname分配给变量,而不是字符串password'Joe'

这是一个可行的示例:

'swordfish'
,

Joe和箭鱼不是会导致错误的字符串。您必须在“ Joe”和“ swordfish”周围加上引号。

,

您要告诉函数在if语句之后继续执行。 if name != 'Joe'如此有效,您希望您的函数continue,但是如果不是,则它将继续正确执行。

这里还有其他问题会让您感到悲伤。您实际上并没有使用input()定义的变量,也没有在询问用户input()的密码。

要进一步简化代码,您可以完全放弃继续语句,而只需确定if name == 'Joe'。如果是这样,则可以要求输入密码。否则,循环可以继续。这是一个简化的解决方案:

while True:
    name = input('Who are you?')
    if name == 'Joe':
        password = input('Hello,Joe. What is the password? (It is a fish.)')  
        if password == 'swordfish':
            print('Access granted.')
            break