python - My answers are 5, 4, 3, 2,but when I run the code ,the answers are 5, 4.Why? -


num = 5 if num > 2:     print(num)     num -= 1 print(num) 

here thoughts:5>2,5,4; 4>2,4,3; 3>2,3,2; 2.so answers 5, 4, 3, 2 when run code,the answers 5 ,4. don't understand it.

use while instead of if.

>>> num = 5 >>> while num > 2: ...     print(num) ...     num -= 1 # 5 # 4 # 3 >>> print(num) # 2 

Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -