if-elif-else結構

鷹擊長空_520發表於2019-07-21

例項:根據年齡段收費的遊樂場。 要求:1、4歲以下免費。 2、4-18歲收費15元。 3、18歲(含)以上收費50元。 程式碼: age=int(input("親,請輸入你的年齡:")) if age<4: print("你就免費暢遊") elif age<18: print("請支付15元門票費用") else: print("請支付50元門票費用")

執行結果: In [22]: age=int(input("親,請輸入你的年齡:")) ...: if age<4: ...: print("你就免費暢遊") ...: elif age<18: ...: print("請支付15元門票費用") ...: else: ...: print("請支付50元門票費用")

親,請輸入你的年齡:12 請支付15元門票費用

相關文章