[英]Why does the first print statement's string gets printed in python?
while True:
print("HI , TELL YOUR NAME")
name = input()
if name != 'anwar':
continue
print("your password , please")
password=input()
if password != 'bull':
break
print("access granted")
So the problem here is when I receive the output, I get the first print statement's string that is所以这里的问题是当我收到 output 时,我得到第一个打印语句的字符串是
--------------- output ------------- --------------- output -------------
HI , TELL YOUR NAME
anwar
your password , please
bull
access granted
HI , TELL YOUR NAME
I dont want the first statement to printed again..我不想再次打印第一条语句..
where as I look into my book and I saw that the same program prints perfectly..当我查看我的书时,我看到相同的程序打印完美..
Python 3.6
1 while True:
2 print('Who are you?')
3 name = input()
4 if name != 'Joe':
5 continue
6 print('Hello, Joe. What is the password? (It is a fish.)')
7 password = input()
8 if password == 'swordfish':
9 break
10 print('Access granted.')
and the output is what I want really wanted........ output 是我真正想要的......
-------------- output ---------- -------------- output ----------
Who are you?
Joe
Hello, Joe. What is the password? (It is a fish.)
swordfish
Access granted.
please note that the first statment "who are you?"请注意第一句话“你是谁?” doesn't print at the end of the program.
在程序结束时不打印。
The correct code is breaking on the right password, while you are breaking if the password doesn't match.正确的密码是在正确的密码上破解,而如果密码不匹配,您就是在破解。 So the it will keep looping until you enter a wrong password
所以它会一直循环,直到你输入错误的密码
if password != 'bull':
should be应该
if password == 'bull':
to break the loop on bull打破公牛的循环
Because you are testing因为你在测试
if password != 'bull':
while your book example uses ==
...虽然您的书示例使用
==
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.