これは私のプログラムのモジュールです:
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 がエラーを示す場所であるとは限らない場合があることを理解しているので、コードを再確認しましたが、私の目には何も問題はありませんでした。文字列要素をテストするためのセットを作成することで、これを迂回していますか? 文字列要素がセットに含まれているかどうかをテストする簡単な方法はありますか?
疑問が解決しました。皆さんありがとうございます!