您好,我是编程新手,我正在尝试编写一个代码来从输入中收集信息并确定它是否是有效的字母表。
这是我到目前为止的代码
words = []
word = input('Character: ')
while word:
if word not in words:
words.append(word)
word = input('Character: ')
print(''.join(words),'is a a valid alphabetical string.')
suppose I choose three letters then the output of my code then pressed enter on the fourth,
the code will be:
Character:a
Character:b
Character:c
Character:
abc is a valid alphabetical string.
I want to add to this code so that when I type in a character that is not
from the alphabet the code will do something like this.
Character:a
Character:b
Character:c
Character:4
4 is not in the alphabet.
这就是我希望我的程序运行的方式
![enter image description here](https://i.stack.imgur.com/k4H12.png)
![enter image description here](https://i.stack.imgur.com/QIAh5.png)
Use str.isalpha()
仅当字符串中的所有字符都是字母时才给出 true。
Example:
>>> 'test'.isalpha()
True
>>> 'test44'.isalpha()
False
>>> 'test test'.isalpha()
False
在你的代码中:
words = []
word = input('Character: ')
while word:
if word.isalpha() and word not in words:
words.append(word)
word = input('Character: ')
print(words,'is a a valid alphabetical string.')
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)