【发布时间】:2020-02-13 21:26:24
【问题描述】:
我需要先返回所有非元音字符,然后返回任何给定字符串中的元音 last。这是我到目前为止所拥有的,首先打印非元音,但之后不打印任何元音:
# Python 3
# Split a string: consonants/any-char (1st), then vowels (2nd)
def split_string():
userInput = input("Type a word here: ")
vowels = "aeiou"
for i in userInput:
if i not in vowels:
print(i, end="")
i += i
# else:
# if i in vowels:
# print(i, end="")
# i = i+i
# This part does not work, so I commented it out for now!
return(userInput)
input = split_string()
回答!我只需要一个 not 嵌套在第一个循环内的第二个循环。
def split_string():
userInput = input("Type a word here: ")
vowels = "aeiou"
for i in userInput:
if i not in vowels:
print(i, end="")
i += i
for i in userInput:
if i in vowels:
print(i, end="")
return(userInput)
input = split_string()
【问题讨论】:
-
你需要两个循环。一个用于非元音。一个用于元音。
-
谢谢,@MateenUlhaq。第二个循环(元音)需要嵌套在第一个循环中,对吗?这就是我遇到的麻烦。
-
不,它们应该是两个独立的循环
-
谢谢!我只需在函数中添加第二个循环就可以让它工作! :)
标签: python python-3.x