我在嘗試運行時遇到語法錯誤,或者有時它運行但沒有按照我想要的方式執行。
我一直在玩弄格式,但仍然沒有解決方案。
def checkVowelsConsonants(s):
vowels=0
consonants=0
for ch in s:
#convert character into its ASCII equivalent
ascii_value=ord(ch)
#if ASCII is between 65 to 90 to 97 to 122 then it's a character
#otherwise a special character
if((ascii_value>=65 and ascii_value<=90)or(ascii_value>=97 and ascii_value<=122)):
#check for lower case
if ch=='a' or ch=='e' or ch=='i' or ch=='o' or ch=='u':
vowels=vowels 1
#check for upper case
elif ch=='A' or ch=='E' or ch=='I' or ch=='O' or ch=='U':
vowels=vowels 1
else:
consonants=consonants 1
#print the result
print("The number of vowels is " str(vowels) " and consonants is " str(consonants))
while True:
#print the menu
print("1. Print the number of vowels and consonats")
print("2. Exit the program")
#take choioce as input from user
choice=int(input("Enter choice: "))
#take sentence input from user
if choice==1:
sentence=input("Enter a sentence: ")
sentence_list=[]
for ch in sentence:
sentence_list.append(ch)
checkVowelsConsonants(sentence_list)
#exit the program
if choice==2:
break
#choice other that 1 and 2
else:
print("Invalid choice!")
uj5u.com熱心網友回復:
我已經清理了你的代碼,用65 <= ascii_value <= 90. 當你想檢查小寫和大寫元音時,我通過檢查是否ch in "aeiouAEIOU"將所有其他有效字符設為小寫或大寫輔音來制作這個 if 條件。
也沒有必要在呼叫sentence之前將用戶輸入轉換為串列checkVowelsConsonants,因為字串也是可迭代的。這就是為什么我洗掉了for附加每個單獨字符的回圈。但是,如果這是您的意圖,請保持原樣。
按照您的代碼,我縮進了它,以便您的回圈僅在iswhile時終止。choice2
def checkVowelsConsonants(s):
vowels = 0
consonants = 0
for ch in s:
# Convert character into its ASCII equivalent
ascii_value = ord(ch)
# If ASCII is between 65 to 90 or 97 to 122 then it's a character
# otherwise a special character
if (65 <= ascii_value <=90) or(97 <= ascii_value <= 122):
# Check for lower case and upper case vowels
if ch in "aeiouAEIOU":
vowels = vowels 1
else:
consonants = consonants 1
# Print the result
print("The number of vowels is " str(vowels) " and consonants is " str(consonants))
while True:
# Print the menu
print("1. Print the number of vowels and consonats")
print("2. Exit the program")
# Take choice as input from user
choice = int(input("Enter choice: "))
# Take sentence input from user
if choice == 1:
sentence = input("Enter a sentence: ")
checkVowelsConsonants(sentence)
# Exit the program
elif choice == 2:
break
# choice other than 1 or 2
else:
print("Invalid choice!")
uj5u.com熱心網友回復:
添加:
即使選擇了選項 1,程式也不會結束。因此,我在這里重寫了代碼,并break在選項輸入后添加了一個。我還使用了字串格式的文本來使內容更簡潔。下面是:
def checkVowelsConsonants(s):
vowels = 0
consonants = 0
for ch in s:
ascii_value = ord(ch)
if ((ascii_value >= 65 & ascii_value <= 90) | (ascii_value >= 97 & ascii_value <= 122)):
#check for lower case
if ch == 'a' or ch == 'e' or ch == 'i' or ch == 'o' or ch == 'u':
vowels = vowels 1
#check for upper case
elif ch == 'A' or ch == 'E' or ch == 'I' or ch == 'O' or ch == 'U':
vowels =1
else:
consonants =1
#print the result
print("The number of vowels is {} and consonants is {}".format(vowels,consonants))
while True:
#print the menu
print("Choices:")
print("1: Print the number of vowels and consonants")
print("2: Exit the program\n")
#take choioce as input from user
choice = int(input("Enter choice: \n"))
#take sentence input from user
if choice == 1:
sentence = input("Enter a sentence: \n")
checkVowelsConsonants(sentence)
break
#exit the program
elif choice == 2:
print('Program exited.')
break
#choice other that 1 and 2
else:
print("Invalid choice!")
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/536601.html
