我的作业分配是编写一个程序,从用户读取一个字符串,并从输入中创建一个单词列表.创建两个列表,一个包含至少包含一个大写字母和一个单词的单词.包含任何大写字母.使用单个for循环打印出带有大写字母的单词,然后是不带大写字母的单词,每行一个单词.
我所知道的不正确:
s= input("Enter your string: ")
words = sorted(s.strip().split())
for word in words:
print (word)
Run Code Online (Sandbox Code Playgroud)
因为只有Capitol在第一个字符时它才会对序列进行排序.对于此分配,角色可以出现在单词中的任何位置.如,'tHis is a sTring'.
我正在玩一个看起来与此类似的解决方案,只是为了看看我是否能用CAPS得到这些话.但它只是不工作:
s = input("Please enter a sentence: ")
while True:
cap = 0
s = s.strip().split()
for c in s:
if c in "ABCDEFGHIJKLMNOPQRSTUVWXYZ":
print(c[:cap])
cap += 1
else:
print("not the answer")
break
Run Code Online (Sandbox Code Playgroud)
但正则表达式可能比写出整个字母表更好.
任何帮助深表感谢.不用说我是python的新手.
python ×1