-3
我是新來的編碼在Python中,需要知道如何只允許用戶在輸入名稱時輸入字母。因此,如果他們輸入一個數字或根本沒有輸入數字,我希望代碼說「請只使用字母,再試一次」。如何在python中尋求名稱時僅允許使用字母?
乾杯克里斯
我是新來的編碼在Python中,需要知道如何只允許用戶在輸入名稱時輸入字母。因此,如果他們輸入一個數字或根本沒有輸入數字,我希望代碼說「請只使用字母,再試一次」。如何在python中尋求名稱時僅允許使用字母?
乾杯克里斯
你所要求的是一個str.isalpha()
功能:
isalpha(...)
S.isalpha() -> bool
Return True if all characters in S are alphabetic
and there is at least one character in S, False otherwise.
例如,你可以使用它像這樣:
def ask_name():
while True:
name = raw_input("What is your name?")
if name.isalpha():
return name
else:
print("Please use only letters, try again")