坚持自动化无聊的东西第 2 章 - 继续声明
Stuck on Automate The Boring Stuff Chapter 2- Continue Statement
我目前正在学习自动化无聊的东西的第 2 章,并且卡在继续语句上。给出的例子是如果输入的名字是'Joe',密码是'swordfish',程序会打印'Access granted'。但是,我不确定为什么当 name = 'Joe' 和 password = 'swordfish' 的条件已经满足时我的仍然打印 'Who are you?' 。谁能告诉我为什么我仍然停留在 while 循环中?
*while True:
print('Who are you?')
name = input()
name = Joe
if name != 'Joe': #if name equal to 'Joe', print('Hello, Joe. What is the password? (It is a fish.)')
continue #if name not equal to 'Joe', continue executing while-loop
print('Hello, Joe. What is the password? (It is a fish.)')
password = swordfish
if password == 'swordfish': #if password equal to 'swordfish', exit the while-loop
break #if password not equal to 'swordfish', execute the while-loop
print('Access granted.')*
Joe 和 swordfish 不是导致错误的字符串。您必须在它们周围添加引号 'Joe' 和 'swordfish'.
您在询问密码时使用的是 print
,而不是 input
。另请注意,您将变量 name
和 password
分配给变量,而不是字符串 'Joe'
和 'swordfish'
.
这是一个工作示例:
while True:
name = input('Who are you?')
if name != 'Joe': # if name equal to 'Joe', print('Hello, Joe. What is the password? (It is a fish.)')
continue # if name not equal to 'Joe', continue executing while-loop
password = input('Hello, Joe. What is the password? (It is a fish.)')
if password == 'swordfish': # if password equal to 'swordfish', exit the while-loop
break # if password not equal to 'swordfish', execute the while-loop
print('Access granted.')
您告诉函数在 if
语句之后继续。如此有效 if name != 'Joe'
你希望你的函数 continue
,但如果不是,它将继续正确执行。
这里还有其他问题会让您感到悲伤。您实际上并没有使用 input()
定义的变量,也没有要求用户 input()
提供密码。
为了进一步简化您的代码,您可以完全去掉 continue 语句,只确定 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
我目前正在学习自动化无聊的东西的第 2 章,并且卡在继续语句上。给出的例子是如果输入的名字是'Joe',密码是'swordfish',程序会打印'Access granted'。但是,我不确定为什么当 name = 'Joe' 和 password = 'swordfish' 的条件已经满足时我的仍然打印 'Who are you?' 。谁能告诉我为什么我仍然停留在 while 循环中?
*while True:
print('Who are you?')
name = input()
name = Joe
if name != 'Joe': #if name equal to 'Joe', print('Hello, Joe. What is the password? (It is a fish.)')
continue #if name not equal to 'Joe', continue executing while-loop
print('Hello, Joe. What is the password? (It is a fish.)')
password = swordfish
if password == 'swordfish': #if password equal to 'swordfish', exit the while-loop
break #if password not equal to 'swordfish', execute the while-loop
print('Access granted.')*
Joe 和 swordfish 不是导致错误的字符串。您必须在它们周围添加引号 'Joe' 和 'swordfish'.
您在询问密码时使用的是 print
,而不是 input
。另请注意,您将变量 name
和 password
分配给变量,而不是字符串 'Joe'
和 'swordfish'
.
这是一个工作示例:
while True:
name = input('Who are you?')
if name != 'Joe': # if name equal to 'Joe', print('Hello, Joe. What is the password? (It is a fish.)')
continue # if name not equal to 'Joe', continue executing while-loop
password = input('Hello, Joe. What is the password? (It is a fish.)')
if password == 'swordfish': # if password equal to 'swordfish', exit the while-loop
break # if password not equal to 'swordfish', execute the while-loop
print('Access granted.')
您告诉函数在 if
语句之后继续。如此有效 if name != 'Joe'
你希望你的函数 continue
,但如果不是,它将继续正确执行。
这里还有其他问题会让您感到悲伤。您实际上并没有使用 input()
定义的变量,也没有要求用户 input()
提供密码。
为了进一步简化您的代码,您可以完全去掉 continue 语句,只确定 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