简体   繁体   中英

Print once in If-Else (Python; please read description)

In a game, I want to print "Good afternoon" once when score is 2 in Python.

if score == 2:
   print("Good afternoon!")

This program prints until score is 2. What should I do to print it once when score reaches 2?

As it's in a game your code is executed in a loop so it will always print("Good afternoon") when the score equals 2.

So you can use a boolean that you may declare in your init function block:

is_score_printed = false

Then:

if score == 2 and not is_score_printed:
    print("Good afternoon!")
    is_score_printed = true

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM