简体   繁体   English

为什么第一个打印语句的字符串会打印在 python 中?

[英]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.

相关问题 为什么当我打印 python 语句时, ( ) 也会被打印出来? - Why when I print a python statement, ( ) gets printed as well? 为什么这样打印字符串? - Why does this string gets printed out like this? 为什么这个字符串弄乱了我的python打印语句 - Why does this string mess with my python print statement 为什么 Python 在这里打印 else 语句? - Why does Python print the else statement here? 当你在 python 中打印某个类的对象时会打印什么? - What gets printed when you print a object of some class in python? 为什么我的python祝福打印语句未在同一位置打印? - Why does my python blessings print statement not print in the same spot? 为什么此打印语句使用 Python f-string output 双括号? - Why does this print statement using a Python f-string output double parentheses? 为什么当我输入 7 、 8 和 9 时会打印“Fail”? - Why does "Fail" gets printed when I input 7 , 8 and 9? 获取一个打印语句两次打印一个值的不同输出,python - Getting a print statement printed twice with different outputs for one value, python 为什么我的打印语句(python)在我的 python 3.4.3 shell 中工作,而不是在线编译器? 是因为版本不同吗? - Why does my print statement(python) work in my python 3.4.3 shell, but not the online compiler? Is it because it's a different version?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM