2016-08-25 3 views

答えて

2

を仕事をしたいかで、これまで

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.') 
+0

最新のアップデートを確認してください – Marzouk

+0

これは私と一緒に、私はpython 3を使って動作します..例外は何ですか? – Marzouk

1

あなたはこれを試してみることができます: -

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 
+0

これは動作しますが、私は問題の2番目の部分にどのように対処しますか – cars

+0

これは両方の問題を解決すると思います –

+0

無数の文字を追加できるようにしたいのですが、このコードは機能しません。 – cars

2

あなたが壊れ、その後、入力を収集するために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 
+0

ユーザが入力なしで入力した場合、もう1つ。どのように私はそれを印刷させるだろう入力は空白です。例:文字:次の行に印刷する入力は空白です。 – cars

+0

ユーザーが入力を押して「___は有効な英数字の文字列です」と表示する方法を区別したいと思いますか?ユーザーが入力して「入力が空です」と表示されます。以前に手紙が入力されていない場合は意味しますか? – Karin

+0

私は上の質問を編集して、わかりやすいダイアグラムを表示します – cars

関連する問題