python:在while循环中仅打印一行1次

时间:2018-10-17 02:30:01

标签: python loops

我正在做一个猜谜游戏,我需要编程以在我的while True循环中打印“仅”行1次。我不想将print(“”)放在循环之外

while True:
       print("I have picked a number between 1 and 100 you can try to guess it.")

3 个答案:

答案 0 :(得分:0)

您可以设置标志

flag = True

while True:
    print("I have picked a number between 1 and 100 you can try to guess it.")
    if flag:
        print("only")
        flag = False

答案 1 :(得分:0)

只需放一个printed bool:

printed = False
while True:
    if not printed:
        print('Hello World!')
        printed = True

答案 2 :(得分:0)

根据文档,您的while外观可能包含else子句

https://docs.python.org/3/reference/compound_stmts.html#the-while-statement

还要查看break语句