こんにちは私はプログラミングが初めてで、入力から情報を収集し、有効なアルファベットであるかどうかを判断するコードを作成しようとしています。ユーザー入力から情報を収集して他の場所に適用する方法
これは、これは私が私のプログラムが
こんにちは私はプログラミングが初めてで、入力から情報を収集し、有効なアルファベットであるかどうかを判断するコードを作成しようとしています。ユーザー入力から情報を収集して他の場所に適用する方法
これは、これは私が私のプログラムが
を仕事をしたいかで、これまで
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.
私のコードで使用str.isalpha()
すべての場合にのみ真与えています文字列内の文字は文字です。
例:あなたのコードで
>>> '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.')
あなたはこれを試してみることができます: -
words = []
while 1:
word = input('Character: ')
if word != '':
try:
if word.isalpha():
pass
if word not in words:
words.append(word)
except Exception:
print word, " is not in the alphabet"
break
else:
res = (''.join(words) +' is a valid alphabetical string.') if (words != []) else "The input was blank."
print res
break
あなたが壊れ、その後、入力を収集するためにwhile
ループを使用することができます入力が空の場合(ユーザーが文字を入力せずに入力した場合)、または入力がアルファベットではない。
letters = []
while True:
letter = input('Character:')
if letter == '':
if letters:
print('{} is a valid alphabetical string.'.format(''.join(letters)))
else:
print('The input was blank.')
break
elif letter.isalpha():
letters.append(letter)
else:
print('{} is not in the alphabet.'.format(letter))
break
最新のアップデートを確認してください – Marzouk
これは私と一緒に、私はpython 3を使って動作します..例外は何ですか? – Marzouk