1

これは私のプログラムのモジュールです:

def runVowels():
      # explains what this program does
    print "This program will count how many vowels and consonants are"
    print "in a string."
      # get the string to be analyzed from user
    stringToCount = input("Please enter a string: ")
      # convert string to all lowercase letters
    stringToCount.lower()
      # sets the index count to it's first number
    index = 0
      # a set of lowercase vowels each element will be tested against
    vowelSet = set(['a','e','i','o','u'])
      # sets the vowel count to 0
    vowels = 0
      # sets the consonant count to 0
    consonants = 0
      # sets the loop to run as many times as there are characters
      # in the string
    while index < len(stringToCount):
          # if an element in the string is in the vowels
        if stringToCount[index] in vowels:
              # then add 1 to the vowel count
            vowels += 1
            index += 1
        # otherwise, add 1 to the consonant count
        elif stringToCount[index] != vowels:
            consonants += 1
            index += 1
          # any other entry is invalid
        else:
            print "Your entry should only include letters."
            getSelection()

      # prints results
    print "In your string, there are:"
    print " " + str(vowels) + " vowels"
    print " " + str(consonants) + " consonants"
      # runs the main menu again
    getSelection()

ただし、このプログラムをテストすると、次のエラーが発生します。

line 28, in runVowels
    stringToCount = input("Please enter a string: ")
  File "<string>", line 1
    PupEman dABest
                 ^
SyntaxError: unexpected EOF while parsing

「while index < len(stringToCount)」に + 1 を追加しようとしましたが、それも役に立ちませんでした。私はPythonにかなり慣れていないので、自分のコードの何が問題なのかよくわかりません。どんな助けでも大歓迎です。

このエラーを調査したところ、EOF はファイルの終わりを表していることがわかりました。これは私の問題を解決するのにまったく役に立ちませんでした。また、エラーが python がエラーを示す場所であるとは限らない場合があることを理解しているので、コードを再確認しましたが、私の目には何も問題はありませんでした。文字列要素をテストするためのセットを作成することで、これを迂回していますか? 文字列要素がセットに含まれているかどうかをテストする簡単な方法はありますか?

疑問が解決しました。皆さんありがとうございます!

4

6 に答える 6

3

Python 2を使用raw_input(...)しているようですinput(...)このinput()関数は、Python 式として入力したものを評価します。これが、SyntaxError を取得した理由です。

于 2013-11-07T22:06:37.647 に答える
2

次のように母音を数えることができます。

>>> st='Testing string against a set of vowels - Python'
>>> sum(1 for c in st if c.lower() in 'aeiou')             
12

子音についても同様のことができます。

>>> sum(1 for c in st if c.lower() in 'bcdfghjklmnpqrstvwxyz')    
26 
于 2013-11-07T22:15:11.247 に答える
1

同じことを解決できる別の方法を次に示します。

def count_vowels_consonants(s):
    return (sum(1 for c in s if c.lower() in "aeiou"),
            sum(1 for c in s if c.lower() in "bcdfghjklmnpqrstvwxyz"))

ウィット:

>>> count_vowels_consonants("aeiou aeiou yyy")
(10, 3)
>>> count_vowels_consonants("hello there")
(4, 6)

Python は本当に素晴らしいです。


ファイルのエラーは次のように実行されます (さらにいくつかの提案):

stringToCount = input("Please enter a string: ")

これはraw_input、ユーザーが文字列として入力したものが必要な場合です。


stringToCount.lower()

この.lower()メソッドは、文字を下げた新しい文字列を返します。オリジナルは変更されません。

>>> a = "HELLO"
>>> a.lower()
"hello"
>>> a
"HELLO"

vowelSet = set(['a','e','i','o','u'])

ここでは、次のように簡単に実行できます。

vowelSet = set("aeiou")

a も厳密には必要ないことに注意してください。ただし、set実際には、一般的にはより効率的です。


  # sets the vowel count to 0
vowels = 0
  # sets the consonant count to 0
consonants = 0

このような単純なステートメントにコメントは必要ありません。


index = 0
while index < len(stringToCount):

通常、Python ではこのような while ループを使用する必要はありません。indexfor を使用するのは、 で対応する文字を取得することだけであることに注意してくださいstringToCount。代わりに次のようにする必要があります。

for c in stringToCount:

代わりに:

    if stringToCount[index] in vowels:
        vowels += 1
        index += 1

あなたはただ行う:

    if c in vowels:
        vowels += 1

    elif stringToCount[index] != vowels:
        consonants += 1
        index += 1
      # any other entry is invalid

正しくありません。文字がセットと等しくないことを確認しています。多分あなたは意味した:

    elif c not in vowels:
        consonants += 1

しかし、その場合はありませんelse... ここでロジックを修正する必要があります。


print "In your string, there are:"
print " " + str(vowels) + " vowels"
print " " + str(consonants) + " consonants"

上記は、よりPython的に次のように書かれています。

print "In your string, there are: %s vowels %s consonants" % (
    vowels, consonants)

# runs the main menu again
getSelection()

なぜあなたがそこにそれを呼んでいるのかわからない - なぜgetSelection()何の呼び出しからも呼び出さないのrunVowel()ですか?


それが役に立ったことを願っています!この素晴らしい言語を楽しく学びましょう。

于 2013-11-07T22:19:13.597 に答える
1

また、

if stringToCount[index] in vowels:

読むべき

if stringToCount[index] in vowelSet:
于 2013-11-07T22:09:53.073 に答える
0

ああ、そのコードはすべてとても遅いです ;)。明らかに最速のソリューションは次のとおりです。

slen = len(StringToCount)
vowels = slen - len(StringToCount.translate(None, 'aeiou'))
consonants = slen - vowels

...私はそれが最も明確であると主張していないことに注意してください...ただ最速です:)

于 2014-11-11T20:44:32.320 に答える