我对python很新,我遇到了麻烦 如果那么其他陈述而我只得到“没有重复的元音”,这意味着我的rep_vowel仍然返回0
所以程序规则如下。
如果旁边没有元音(例如hello),则打印:
no vowel repeats
如果只有一个元音按顺序重复至少一次(例如委员会),则打印一条消息,指示哪个元音重复:
only vowel e repeats
如果重复多个元音(例如绿门),则打印:
more than one vowel repeats
忽略大写 - 小写差异:假设所有输入始终为小写
answer = input("Enter a string: ")
rep_vowel = 0
i = 0
length_Answer = len(answer)
next_string = 1
curChar = answer[0+rep_vowel]
for i in range(0,length_Answer):
if answer[0 + i] in ["a","e","i","o","u"]:
i =+ 1
next_string = answer[0+i+i]
if next_string == answer:
rep_vowel =+ 1
if rep_vowel == 0:
print("no repeating vowles")
elif rep_vowel > 1:
print("more than 1 repeating vowels")
else:
print ("the letter "+ str(curChar) +" repeats")
答案 0 :(得分:2)
你有一些错误,所以我会试着解决其中几个错误:
你做了很多[0 + something]
索引,这是没用的,因为0 + something
总是等于something
,所以你应该只用[something]
<做索引/ p>
使用i
更改i += 1
的值是不好的,因为您已经将其作为循环的一部分增加
你要做的就是找到一个匹配只是将当前的字母与下一个字母匹配,如果两者相同而且它们也是元音,你就找到了匹配。
您正在初始化不必要的变量,例如i = 0
,只是为了让它们在下一行中被覆盖
将所有这些加在一起:
answer = input("Enter a string: ")
vowels = "aeiou"
repeats = [] # this list will hold all repeats of vowels
for i in range(len(answer) - 1): # i'll explain the -1 part at the end
if answer[i] in vowels and answer[i] == answer[i + 1]:
repeats.append(answer[i])
if len(repeats) == 0:
print("no repeating vowles")
elif len(repeats) > 1:
print("more than 1 repeating vowels")
else:
print("the letter " + repeats[0] + " repeats")
这仍然没有考虑到所有可能的输入,但它应该让你开始最终的解决方案(或者这可能就足够了)。例如,teest
的输入将给出正确的结果,但teeest
的输入不会(取决于您的正确定义)。
关于len(answer-1)
范围,这只是为了确保我们在执行answer[i + 1]
时不会超出范围,所以我们会停在最后一个字母的旁边。
答案 1 :(得分:1)
您有一些逻辑错误。编辑它是耗时的。你可以尝试这个,我修改了你的代码。希望它能为你效劳。我在每一条重要的路线上都做了评论。
answer = input("Enter a string: ")
is_found = {} #a dictionary that will hold information about how many times a vowel found,initially all are 0
is_found["a"]=0
is_found["e"] = 0
is_found['i']=0
is_found['o']=0
is_found['u']=0
vowels =["a","e","i","o","u"]
for i in range(0,len(answer)):
if answer[i] in vowels:
is_found[answer[i]] = is_found[answer[i]]+1 # if a vowel found then increase its counter
repeated=0 #let 0 repeated vowel
previously_repeated=False #to trace whether there is a previously repeated character found
curChar=None
for key,value in is_found.items(): #iterate over dictionary
if previously_repeated and value>1: #if a vowel found and previously we have another repeated vowel.
repeated=2
elif previously_repeated==False and value>1: # we don't have previously repeated vowel but current vowel is repeated
curChar=key
previously_repeated=True
repeated=1
if repeated== 0:
print("no repeating vowles")
elif repeated> 1:
print("more than 1 repeating vowels")
else:
print ("the letter "+ str(curChar) +" repeats")
答案 2 :(得分:1)
您的解决方案存在一些问题:
1)你从不使用curChar,我猜你想在'=='语句之后输入next_string值。
2)你比较你的next_string来回答,这将永远是一个错误的陈述。
3)也不需要使用[0 + i],[i]足够好
基本上你想要做的就是这个流程:
1)读取当前字符
2)与下一个char比较
3)如果等于放入不同的变量
4)如果再次发生举旗
可选解决方案:
vowel_list = ["a","e","i","o","u"]
recuring_vowel_boolean_list = [answer[index]==answer[index+1] and answer[index] in vowel_list for index in range(len(answer)-1)]
if not any(recuring_vowel_boolean_list ):
print("no repeating vowels")
elif (recuring_vowel_boolean_list.count(True) > 1):
print("More then 1 repeating vowels")
else:
print("The letter {} repeats".format(answer[recuring_vowel_boolean_list.index(True)]))
答案 3 :(得分:1)
没有必要增加你的计数器i。在for循环中,每次进入for循环时它都会递增。此外,您需要一个变量来跟踪元音重复的次数。
answer = input("Enter a string: ")
rep_vowel = 0
length_Answer = len(answer)
vowelList=["a","e","i","o","u"]
vowelRepeated = []
#this will go from i=0 to length_Answer-1
for i in range(length_Answer):
if (answer[i] in vowelList) and (answer[i+1] in vowelList):
if (answer[i] == answer[i+1]):
vowelRepeated.append(answer[i])
repVowel += 1
if rep_vowel==0:
print("no repeating vowels")
elif rep_vowel==1:
print("only one vowel repeated:")
print(vowelRepeated)
else:
print("multiple vowels repeated:")
print(vowelRepeated)
答案 4 :(得分:1)
对于这样的计数,我宁愿使用字典来保存计数。您的代码已经过修改以供参考
answer = input("Enter a string: ")
length_Answer = len(answer)
count = dict()
for i in range(length_Answer):
if answer[i] in ["a","e","i","o","u"]:
if answer[i+1] == answer[i]:
if answer[i] in count:
count[answer[i]] += 1
else:
count[answer[i]] = 1
rep_vowel = len(count)
if rep_vowel == 0:
print("no repeating vowles")
elif rep_vowel > 1:
print("more than 1 repeating vowels")
else:
for k in count:
vowel = k
print("the letter " + vowel + " repeats")
答案 5 :(得分:1)
首先,您必须缩进代码。 如果(条件)然后打印(&#39;你好&#39;)你这样写:
type="text"
其次,您使用的if condition:
print('hello')
与i =+ 1
相同
我认为你的意思是i=1
i +=1
最后,我建议这段代码:
i = i+1