在這個簡單的 python 程序中,我們需要檢查給定的數據是否是英文字母。這是一個初級 python 程序。
為了更好地理解這個例子,我們總是建議您學習下面列出的 Python 編程的基本主題:
- Python 語法
- Python 數據類型
- Python 決策語句
python 中如何檢查輸入是不是字母表?
這個 python 程序是為了檢查用戶輸入的是字母表還是其他什麼。在正常情況下,用戶必須輸入數字或特殊字元或大寫或小寫字母。所以基本邏輯是檢查條件,就好像用戶輸入是在小寫「 a 和「 z 之間或者大寫「 A 和「 Z 之間」。如果是這樣,我們必須把它列印成字母表,否則它就不是字母表。我們可以應用這個邏輯,因為每個字母表都有一個 ASCII 值,所以我們可以很容易地在 python 中使用小於和大於if
條件。
演算法
STEP 1: 使用 python 語言的輸入函數接受用戶的輸入值,並將該值存儲在變數中。
步驟 2: 使用if
條件檢查輸入值是在「 a 和「 z 之間還是在「 A 和「 Z 之間」。我們應用字母表的 ASCII 值來應用 if 條件。
步驟 3: 列印結果,因為輸入的輸入值是字母表。
STEP 4: 使用 python 編程中的else
語句,列印輸入值不是字母表。
Python 源代碼
ch = input("Enter any input value: ")
if((ch>='a' and ch<= 'z') or (ch>='A' and ch<='Z')): # checking the condition using the if statement as the entered value is between the alphabet ASCII value.
print(ch, "Entered value is an Alphabet")
else:
print(ch, "Entered value is not an Alphabet")
輸出
Enter any input value: J
J Entered value is an Alphabet
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/256357.html