x = input()
vowels = ['a', 'e', 'i', 'o', 'u']
我正在尝试使用if
语句来显示字符串中是否包含y
(不知道y
在字符串中的位置)。
如果y
之前有任何元音,print(1)
;如果不是print (2)
,并且单词y
中没有print(3)
。
答案 0 :(得分:1)
import re
word = "wordy"
ypos = word.find("y")
if ypos != -1:
if(bool(re.search(('|'.join(["a","e","i","o","u"])),word[:ypos]))):
print("1")
else:
print("2")
else :
print("3")
答案 1 :(得分:1)
>>> def y_in_word(word):
... if 'y' not in word:
... print 3
... elif any([x in word for x in ['ay', 'ey', 'iy', 'oy', 'uy']]):
... print 2
... else:
... print 1
...
>>>
>>> y_in_word('wordy')
1
>>> y_in_word('worday')
2
>>> y_in_word('worda')
3
>>>
答案 2 :(得分:0)
不使用正则表达式:
x = input()
vowels = "aeiou"
for i in range(len(x)):
if x[i] == "y":
break
if x[i] != "y": # No y found
print(3)
else:
flag = 0
new_str = x[:i+1]
for ch in new_str:
if ch in vowels:
print(1)
flag += 1
break
if flag == 0:
print(2)
答案 3 :(得分:0)
x = input()
vowels = ['a', 'e', 'i', 'o', 'u']
if 'y' in x:
# find the position of 'y' in the input string
y_position = x.index(y)
# loop through each vowel
for vowel in vowels:
# if the vowel is in the input string and its position is before y
if vowel in x and x.index(vowel) < y_position:
print(1)
break
# if we made it all the way through the loop without breaking, we did not find
# any vowels before y
else:
print(2)
else:
print(3)
答案 4 :(得分:0)
先变小写然后再判断
import re
x = input().lower()
m = re.findall("(?=a|e|i|o|u)\w*?(?=y)",x)
if "y" in x.lower():
if m:
print(1)
else:
print(2)
else:
print(3)