Giải pháp chính xác sẽ sử dụng or
.
string = input["Enter a string: "]
if all[x.isalpha[] or x.isspace[] for x in string]:
print["Only alphabetical letters and spaces: yes"]
else:
print["Only alphabetical letters and spaces: no"]
Mặc dù bạn có một chuỗi, bạn đang lặp lại các chữ cái của chuỗi đó, vì vậy bạn có một chữ cái cùng một lúc. Vì vậy, một mình char không thể là một nhân vật bảng chữ cái và một không gian vào thời điểm đó, nhưng nó sẽ chỉ cần là một trong hai để thỏa mãn ràng buộc của bạn.
Chỉnh sửa: Tôi đã thấy nhận xét của bạn trong câu trả lời khác. alphabet = string.isalpha[]
Trả về True
, nếu và chỉ khi tất cả các ký tự trong chuỗi là chữ cái theo thứ tự chữ cái. Đây không phải là những gì bạn muốn, bởi vì bạn đã nói rằng bạn muốn mã của mình in yes
khi thực thi với chuỗi ____10, có không gian. Bạn cần phải tự kiểm tra từng chữ cái, không phải toàn bộ chuỗi. I saw your comment in the other answer. alphabet = string.isalpha[]
return True
, if and only if all characters in a string are
alphabetical letters. This is not what you want, because you stated that you want your code to print yes
when execute with the string
>>> string = "please "
>>> if all[x.isalpha[] or x.isspace[] for x in string]:
print["Only alphabetical letters and spaces: yes"]
else:
print["Only alphabetical letters and spaces: no"]
Only alphabetical letters and spaces: yes
0, which has a space. You need to check each letter on its own, not the whole string.Chỉ cần thuyết phục bạn rằng mã thực sự là chính xác [tốt, ok, bạn cần tự thực hiện nó để bị thuyết phục, nhưng dù sao]
>>> string = "please "
>>> if all[x.isalpha[] or x.isspace[] for x in string]:
print["Only alphabetical letters and spaces: yes"]
else:
print["Only alphabetical letters and spaces: no"]
Only alphabetical letters and spaces: yes
EDIT 2: Đánh giá từ nhận xét mới của bạn, bạn cần một cái gì đó như thế này: Judging from your new comments, you need something like this:
def hasSpaceAndAlpha[string]:
return any[char.isalpha[] for char in string] and any[char.isspace[] for char in string] and all[char.isalpha[] or char.isspace[] for char in string]
>>> hasSpaceAndAlpha["text# "]
False
>>> hasSpaceAndAlpha["text"]
False
>>> hasSpaceAndAlpha["text "]
True
hoặc
def hasSpaceAndAlpha[string]:
if any[char.isalpha[] for char in string] and any[char.isspace[] for char in string] and all[char.isalpha[] or char.isspace[] for char in string]:
print["Only alphabetical letters and spaces: yes"]
else:
print["Only alphabetical letters and spaces: no"]
>>> hasSpaceAndAlpha["text# "]
Only alphabetical letters and spaces: no
>>> hasSpaceAndAlpha["text"]
Only alphabetical letters and spaces: no
>>> hasSpaceAndAlpha["text "]
Only alphabetical letters and spaces: yes
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc
Bàn luận
This is one of the way in which this task can be performed. In this, we compare the string for all elements being alphabets or space only.
Python3
>>> string = "please "
>>> if all[x.isalpha[] or x.isspace[] for x in string]:
print["Only alphabetical letters and spaces: yes"]
else:
print["Only alphabetical letters and spaces: no"]
Only alphabetical letters and spaces: yes
1 >>> string = "please "
>>> if all[x.isalpha[] or x.isspace[] for x in string]:
print["Only alphabetical letters and spaces: yes"]
else:
print["Only alphabetical letters and spaces: no"]
Only alphabetical letters and spaces: yes
2>>> string = "please "
>>> if all[x.isalpha[] or x.isspace[] for x in string]:
print["Only alphabetical letters and spaces: yes"]
else:
print["Only alphabetical letters and spaces: no"]
Only alphabetical letters and spaces: yes
3>>> string = "please "
>>> if all[x.isalpha[] or x.isspace[] for x in string]:
print["Only alphabetical letters and spaces: yes"]
else:
print["Only alphabetical letters and spaces: no"]
Only alphabetical letters and spaces: yes
4 >>> string = "please "
>>> if all[x.isalpha[] or x.isspace[] for x in string]:
print["Only alphabetical letters and spaces: yes"]
else:
print["Only alphabetical letters and spaces: no"]
Only alphabetical letters and spaces: yes
5>>> string = "please "
>>> if all[x.isalpha[] or x.isspace[] for x in string]:
print["Only alphabetical letters and spaces: yes"]
else:
print["Only alphabetical letters and spaces: no"]
Only alphabetical letters and spaces: yes
6>>> string = "please "
>>> if all[x.isalpha[] or x.isspace[] for x in string]:
print["Only alphabetical letters and spaces: yes"]
else:
print["Only alphabetical letters and spaces: no"]
Only alphabetical letters and spaces: yes
7>>> string = "please "
>>> if all[x.isalpha[] or x.isspace[] for x in string]:
print["Only alphabetical letters and spaces: yes"]
else:
print["Only alphabetical letters and spaces: no"]
Only alphabetical letters and spaces: yes
8 >>> string = "please "
>>> if all[x.isalpha[] or x.isspace[] for x in string]:
print["Only alphabetical letters and spaces: yes"]
else:
print["Only alphabetical letters and spaces: no"]
Only alphabetical letters and spaces: yes
9 def hasSpaceAndAlpha[string]:
return any[char.isalpha[] for char in string] and any[char.isspace[] for char in string] and all[char.isalpha[] or char.isspace[] for char in string]
>>> hasSpaceAndAlpha["text# "]
False
>>> hasSpaceAndAlpha["text"]
False
>>> hasSpaceAndAlpha["text "]
True
0def hasSpaceAndAlpha[string]:
return any[char.isalpha[] for char in string] and any[char.isspace[] for char in string] and all[char.isalpha[] or char.isspace[] for char in string]
>>> hasSpaceAndAlpha["text# "]
False
>>> hasSpaceAndAlpha["text"]
False
>>> hasSpaceAndAlpha["text "]
True
1>>> string = "please "
>>> if all[x.isalpha[] or x.isspace[] for x in string]:
print["Only alphabetical letters and spaces: yes"]
else:
print["Only alphabetical letters and spaces: no"]
Only alphabetical letters and spaces: yes
4 def hasSpaceAndAlpha[string]:
return any[char.isalpha[] for char in string] and any[char.isspace[] for char in string] and all[char.isalpha[] or char.isspace[] for char in string]
>>> hasSpaceAndAlpha["text# "]
False
>>> hasSpaceAndAlpha["text"]
False
>>> hasSpaceAndAlpha["text "]
True
3>>> string = "please "
>>> if all[x.isalpha[] or x.isspace[] for x in string]:
print["Only alphabetical letters and spaces: yes"]
else:
print["Only alphabetical letters and spaces: no"]
Only alphabetical letters and spaces: yes
4 def hasSpaceAndAlpha[string]:
return any[char.isalpha[] for char in string] and any[char.isspace[] for char in string] and all[char.isalpha[] or char.isspace[] for char in string]
>>> hasSpaceAndAlpha["text# "]
False
>>> hasSpaceAndAlpha["text"]
False
>>> hasSpaceAndAlpha["text "]
True
5def hasSpaceAndAlpha[string]:
return any[char.isalpha[] for char in string] and any[char.isspace[] for char in string] and all[char.isalpha[] or char.isspace[] for char in string]
>>> hasSpaceAndAlpha["text# "]
False
>>> hasSpaceAndAlpha["text"]
False
>>> hasSpaceAndAlpha["text "]
True
6 def hasSpaceAndAlpha[string]:
return any[char.isalpha[] for char in string] and any[char.isspace[] for char in string] and all[char.isalpha[] or char.isspace[] for char in string]
>>> hasSpaceAndAlpha["text# "]
False
>>> hasSpaceAndAlpha["text"]
False
>>> hasSpaceAndAlpha["text "]
True
7>>> string = "please "
>>> if all[x.isalpha[] or x.isspace[] for x in string]:
print["Only alphabetical letters and spaces: yes"]
else:
print["Only alphabetical letters and spaces: no"]
Only alphabetical letters and spaces: yes
7__>>> string = "please "
>>> if all[x.isalpha[] or x.isspace[] for x in string]:
print["Only alphabetical letters and spaces: yes"]
else:
print["Only alphabetical letters and spaces: no"]
Only alphabetical letters and spaces: yes
6>>> string = "please "
>>> if all[x.isalpha[] or x.isspace[] for x in string]:
print["Only alphabetical letters and spaces: yes"]
else:
print["Only alphabetical letters and spaces: no"]
Only alphabetical letters and spaces: yes
7The original string is : geeksforgeeks is best for geeks Does String contain only space and alphabets : True0
>>> string = "please "
>>> if all[x.isalpha[] or x.isspace[] for x in string]:
print["Only alphabetical letters and spaces: yes"]
else:
print["Only alphabetical letters and spaces: no"]
Only alphabetical letters and spaces: yes
9 The original string is : geeksforgeeks is best for geeks Does String contain only space and alphabets : True2
The original string is : geeksforgeeks is best for geeks Does String contain only space and alphabets : True3
Đầu ra: & nbsp;
The original string is : geeksforgeeks is best for geeks Does String contain only space and alphabets : True
Phương pháp số 1: Sử dụng Regex & NBSP; vấn đề này cũng có thể được giải quyết bằng cách sử dụng Regex để chỉ bao gồm không gian và bảng chữ cái trong một chuỗi.
This problem can also be solved by employing regex to include only space and alphabets in a string.
Python3
>>> string = "please "
>>> if all[x.isalpha[] or x.isspace[] for x in string]:
print["Only alphabetical letters and spaces: yes"]
else:
print["Only alphabetical letters and spaces: no"]
Only alphabetical letters and spaces: yes
1 >>> string = "please "
>>> if all[x.isalpha[] or x.isspace[] for x in string]:
print["Only alphabetical letters and spaces: yes"]
else:
print["Only alphabetical letters and spaces: no"]
Only alphabetical letters and spaces: yes
2>>> string = "please "
>>> if all[x.isalpha[] or x.isspace[] for x in string]:
print["Only alphabetical letters and spaces: yes"]
else:
print["Only alphabetical letters and spaces: no"]
Only alphabetical letters and spaces: yes
3>>> string = "please "
>>> if all[x.isalpha[] or x.isspace[] for x in string]:
print["Only alphabetical letters and spaces: yes"]
else:
print["Only alphabetical letters and spaces: no"]
Only alphabetical letters and spaces: yes
4 >>> string = "please "
>>> if all[x.isalpha[] or x.isspace[] for x in string]:
print["Only alphabetical letters and spaces: yes"]
else:
print["Only alphabetical letters and spaces: no"]
Only alphabetical letters and spaces: yes
5>>> string = "please "
>>> if all[x.isalpha[] or x.isspace[] for x in string]:
print["Only alphabetical letters and spaces: yes"]
else:
print["Only alphabetical letters and spaces: no"]
Only alphabetical letters and spaces: yes
6>>> string = "please "
>>> if all[x.isalpha[] or x.isspace[] for x in string]:
print["Only alphabetical letters and spaces: yes"]
else:
print["Only alphabetical letters and spaces: no"]
Only alphabetical letters and spaces: yes
7>>> string = "please "
>>> if all[x.isalpha[] or x.isspace[] for x in string]:
print["Only alphabetical letters and spaces: yes"]
else:
print["Only alphabetical letters and spaces: no"]
Only alphabetical letters and spaces: yes
8 >>> string = "please "
>>> if all[x.isalpha[] or x.isspace[] for x in string]:
print["Only alphabetical letters and spaces: yes"]
else:
print["Only alphabetical letters and spaces: no"]
Only alphabetical letters and spaces: yes
9 def hasSpaceAndAlpha[string]:
return any[char.isalpha[] for char in string] and any[char.isspace[] for char in string] and all[char.isalpha[] or char.isspace[] for char in string]
>>> hasSpaceAndAlpha["text# "]
False
>>> hasSpaceAndAlpha["text"]
False
>>> hasSpaceAndAlpha["text "]
True
0def hasSpaceAndAlpha[string]:
return any[char.isalpha[] for char in string] and any[char.isspace[] for char in string] and all[char.isalpha[] or char.isspace[] for char in string]
>>> hasSpaceAndAlpha["text# "]
False
>>> hasSpaceAndAlpha["text"]
False
>>> hasSpaceAndAlpha["text "]
True
1>>> string = "please "
>>> if all[x.isalpha[] or x.isspace[] for x in string]:
print["Only alphabetical letters and spaces: yes"]
else:
print["Only alphabetical letters and spaces: no"]
Only alphabetical letters and spaces: yes
4 def hasSpaceAndAlpha[string]:
return any[char.isalpha[] for char in string] and any[char.isspace[] for char in string] and all[char.isalpha[] or char.isspace[] for char in string]
>>> hasSpaceAndAlpha["text# "]
False
>>> hasSpaceAndAlpha["text"]
False
>>> hasSpaceAndAlpha["text "]
True
3>>> string = "please "
>>> if all[x.isalpha[] or x.isspace[] for x in string]:
print["Only alphabetical letters and spaces: yes"]
else:
print["Only alphabetical letters and spaces: no"]
Only alphabetical letters and spaces: yes
4 def hasSpaceAndAlpha[string]:
return any[char.isalpha[] for char in string] and any[char.isspace[] for char in string] and all[char.isalpha[] or char.isspace[] for char in string]
>>> hasSpaceAndAlpha["text# "]
False
>>> hasSpaceAndAlpha["text"]
False
>>> hasSpaceAndAlpha["text "]
True
5def hasSpaceAndAlpha[string]:
return any[char.isalpha[] for char in string] and any[char.isspace[] for char in string] and all[char.isalpha[] or char.isspace[] for char in string]
>>> hasSpaceAndAlpha["text# "]
False
>>> hasSpaceAndAlpha["text"]
False
>>> hasSpaceAndAlpha["text "]
True
6 def hasSpaceAndAlpha[string]:
return any[char.isalpha[] for char in string] and any[char.isspace[] for char in string] and all[char.isalpha[] or char.isspace[] for char in string]
>>> hasSpaceAndAlpha["text# "]
False
>>> hasSpaceAndAlpha["text"]
False
>>> hasSpaceAndAlpha["text "]
True
7>>> string = "please "
>>> if all[x.isalpha[] or x.isspace[] for x in string]:
print["Only alphabetical letters and spaces: yes"]
else:
print["Only alphabetical letters and spaces: no"]
Only alphabetical letters and spaces: yes
7__>>> string = "please "
>>> if all[x.isalpha[] or x.isspace[] for x in string]:
print["Only alphabetical letters and spaces: yes"]
else:
print["Only alphabetical letters and spaces: no"]
Only alphabetical letters and spaces: yes
6>>> string = "please "
>>> if all[x.isalpha[] or x.isspace[] for x in string]:
print["Only alphabetical letters and spaces: yes"]
else:
print["Only alphabetical letters and spaces: no"]
Only alphabetical letters and spaces: yes
7The original string is : geeksforgeeks is best for geeks Does String contain only space and alphabets : True0
>>> string = "please "
>>> if all[x.isalpha[] or x.isspace[] for x in string]:
print["Only alphabetical letters and spaces: yes"]
else:
print["Only alphabetical letters and spaces: no"]
Only alphabetical letters and spaces: yes
9 The original string is : geeksforgeeks is best for geeks Does String contain only space and alphabets : True2
The original string is : geeksforgeeks is best for geeks Does String contain only space and alphabets : True3
Đầu ra: & nbsp;
The original string is : geeksforgeeks is best for geeks Does String contain only space and alphabets : True
Phương pháp số 1: Sử dụng Regex & NBSP; vấn đề này cũng có thể được giải quyết bằng cách sử dụng Regex để chỉ bao gồm không gian và bảng chữ cái trong một chuỗi.
1def hasSpaceAndAlpha[string]:
return any[char.isalpha[] for char in string] and any[char.isspace[] for char in string] and all[char.isalpha[] or char.isspace[] for char in string]
>>> hasSpaceAndAlpha["text# "]
False
>>> hasSpaceAndAlpha["text"]
False
>>> hasSpaceAndAlpha["text "]
True
4 >>> string = "please "
>>> if all[x.isalpha[] or x.isspace[] for x in string]:
print["Only alphabetical letters and spaces: yes"]
else:
print["Only alphabetical letters and spaces: no"]
Only alphabetical letters and spaces: yes
The original string is : geeksforgeeks is best for geeks
Does String contain only space and alphabets : True
6The original string is : geeksforgeeks is best for geeks
Does String contain only space and alphabets : True
7The original string is : geeksforgeeks is best for geeks
Does String contain only space and alphabets : True
8The original string is : geeksforgeeks is best for geeks
Does String contain only space and alphabets : True
9O[n]
Độ phức tạp về thời gian và không gian cho tất cả các phương pháp là như nhau:O[n]