2016-06-25 8 views
0
sentence = input("What is your sentence?") 
sentence=sentence.capitalize() 
counta = sentence.count("a") 
counte = sentence.count("e") 
counti = sentence.count("i") 
counto= sentence.count ("o") 
countu= sentence.count ("u") 
countA2 = sentence.count("A") 
countE2 = sentence.count("E") 
countI2 = sentence.count("I") 
countO2 = sentence.count("O") 
countU2 = sentence.count("U") 
countI3 = sentence.count(" I ") 
countspaces = sentence.count(" ") 
a1 = sentence.count("!") 
a2 = sentence.count(".") 
a3 = sentence.count(">") 
a4 = sentence.count("<") 
a5 = sentence.count(":") 
a6= sentence.count(";") 
a7 = sentence.count("'") 
a8 = sentence.count("@") 
a9 = sentence.count("#") 
a10 = sentence.count("~") 
a11= sentence.count("{") 
a12= sentence.count("}") 
a13= sentence.count("[") 
a14 = sentence.count("]") 
a15 = sentence.count("-") 
a16 = sentence.count("_") 
a17 = sentence.count("+") 
a18 = sentence.count("=") 
a19 = sentence.count("£") 
a20 = sentence.count("$") 
a21= sentence.count("%") 
a22 = sentence.count("^") 
a23= sentence.count("&") 
a24 = sentence.count("(") 
a25= sentence.count(")") 
a26=sentence.count("?") 
count = (counta + counte + counti + counto + countu + countA2 + countE2 +  countI2 + countO2 + countU2 + countI3) 
speci= a1+a2+a3+a4+a5+a6+a7+a8+a9+a10+a11+a12+a13+a14+a15+a16+a17+a18+a19+a20+a21+a22+a23+a24+a25+a26) 
print(sentence) 
print("This has", speci, "special characters") 
print("This has", countspaces, "Spaces") 
print("This has", count, "vowels") 

ご覧のとおり、上記のコードは長すぎます。私は同じこと(ユーザ入力文であり、母音、空白、特殊文字を数えます)を行う方法は分かりませんが、コードの行数は少なくなります。あなたがより良い方法を知っているなら。教えてください。もちろん、コーディングはすべて最高のコードに関するものであり、これは最良の方法であるために奇妙に見えます。キーとして母音で辞書を作る、母音の場合このコードの長さを短くする方法はありますか?同じことをしますか?

spaces = sentence.count(' ') 

、その後、行く:スペースの場合

special_char = 0 
for i in sentence: 
    if i.isalpha() == False and i.isdigit() == False: 
     special_char += 1 

を:あなたの助け

+6

です優れたことをしたい。改善の準備ができている人はたくさんいますが、[codereview.se]で待ち合わせをしています。そこに幸運!また、あなたのコードが何をしているのかを説明して、よりよくレビューできるようにするのも良い考えです。 – zondo

+1

私は、それぞれのケース(例えば、 'vowels = 'aeiou')に対して文字列を持つことから始め、現在の暗黙的ではなく入力に対して繰り返します(' for char in sentence.lower(): ')あなたが探している文字の繰り返し。 – jonrsharpe

+0

また、 'from collections import Counter'と' Counter( 'これは例文です')の結果に注意してください。これは役に立つかもしれません。 – jonrsharpe

答えて

2

ため おかげであなたは特別な文字をチェックしたい場合は、行います入力全体を通して、母音辞書から個々のキーとの一致を実行します。 EMすべてを支配する

0

ワンループ:

sentence = raw_input() 
vowels = special_char = spaces = 0 
for letter in sentence.lower(): 
    if letter in ['a', 'e', 'i', 'o', 'u']: 
     vowels += 1 
    elif letter == ' ': 
     spaces += 1 
    elif not (letter.isalpha() or letter.isdigit()): 
     special_char += 1 
print("Vowels: {},Spaces: {},Special Chars: {}".format(vowels,spaces,special_char)) 
+0

これは、無効な構文を返します。 ^ SyntaxError:無効な構文 – jennmaurice

+0

'print'文は自分のマシンで正常に動作します。あなたはPython 3を使用していますか?私は編集を行いました。うまくいけば構文エラーが発生することはありません。見てみな。 –

0

は、ソリューションを見て、あなたはおそらくすでにあなたの心を占めています。使用する個々の文字を選択して短縮するには、以下のコードを試してください。

word = "banana" 
characters = ["A", "E", "I", "O", "U"] 
for letter in word: 
    if letter in characters: 
     print("Special character.") 

ただ、もちろん私は母音を追加したのと同じ方法を使用して、リストに複数の文字を追加

0

をする場合には、あなたのリストの内包表記のような場合:。

sentence = raw_input("What is your sentence ? ") 
print "Vowels : " + str(len([i for i in sentence if i.lower() in ('a','e','i','o','u')])) 
print "Spaces : " + str(sentence.count(' ')) 
print "Special Chars : " + str(len([i for i in sentence if (not i.isalpha() and not i.isdigit() and not i.isspace())]))