Skip to content

Python 201 偶數判斷

Python TQC

題目說明:

請使用選擇敘述撰寫一程式,讓使用者輸入一個正整數,然後判斷它是否為偶數(even)。

範例輸入1
56

範例輸出1

56 is an even number.

範例輸入2

21

範例輸出2

21 is not an even number.

題目解析

  1. 首先,程序需要接受用戶的輸入,這里要求用戶輸入一個正整數。
  2. 接著,程序需要判斷用戶輸入的整數是否為偶數。
  3. 最後,根據判斷結果,程序要輸出相應的信息,如果是偶數,則輸出”xx is an even number.”,否則輸出”xx is not an even number.”,其中 xx 是用戶輸入的整數。

在編寫代碼時,我們需要注意以下幾點:

  • 需要使用輸入函數來接受用戶的輸入,例如 input() 函數。
  • 需要使用條件語句來進行判斷,以便根據判斷結果輸出相應的信息,例如 if...else 結構。
  • 需要使用格式化字符串來輸出結果,以便將用戶輸入的整數和判斷結果整合到輸出語句中。

Solution

1
2
3
4
5
a = eval(input())
if (a % 2 == 0):
    print("{} is an even number.".format(a))
else:
    print("{} is not an even number.".format(a))

Last update : 13 novembre 2024
Created : 13 novembre 2024

Comments

Comments