簡體   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")

所以這里的問題是當我收到 output 時,我得到第一個打印語句的字符串是

--------------- output -------------

HI , TELL YOUR NAME
anwar
your password , please
bull
access granted
HI , TELL YOUR NAME

我不想再次打印第一條語句..

當我查看我的書時,我看到相同的程序打印完美..

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.')

output 是我真正想要的......

-------------- output ----------

Who are you?
Joe
Hello, Joe. What is the password? (It is a fish.)
swordfish
Access granted.

請注意第一句話“你是誰?” 在程序結束時不打印。

正確的密碼是在正確的密碼上破解,而如果密碼不匹配,您就是在破解。 所以它會一直循環,直到你輸入錯誤的密碼

if password != 'bull':

應該

if password == 'bull':

打破公牛的循環

因為你在測試

if password != 'bull':

雖然您的書示例使用== ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM