一個簡單例子闡明while True與if的配合使用方法


#!/usr/bin/env python
oldboy_age = 56
#while True 是個死循環,需要用break來退出
while True:
    get_num = int(input("Pls guess oldboy\'s age:"))
    if get_num == oldboy_age:
        print("Oldboy is {age} years old.".format(age=get_num))
        break
    elif get_num > oldboy_age:
        print("Your input is larger than his age.")
    else:
        print("Your input is smaller than his age.")

運行結果:

D:\Python\Python36\python.exe "E:/python 14/day0101.py"
Pls guess oldboy's age:99
Your input is larger than his age.
Pls guess oldboy's age:22
Your input is smaller than his age.
Pls guess oldboy's age:56
Oldboy is 56 years old.

Process finished with exit code 0

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM