我需要使用函数以相反的顺序打印用户的输入。作为一个条件,只允许字(无浮点数/int);至少需要输入四个单词。 例如。: 我能为您效劳吗 --&燃气轮机;你能帮我吗
不能输入“4 5 6 7”或“有2条狗” 这是我当前的代码,但是我没有集成到目前为止只允许使用字符串:
def phrase():
while True:
user_input = input("Please insert a phrase: ")
words = user_input.split(" ")
n = len(words)
if n >= 4:
words = words[-1::-1]
phrase_reverse = " ".join(words)
print(phrase_reverse)
else:
print("Please only insert words and at least 4 ")
continue
break
phrase()
我试过if n<4 and words == str:
、if n<4 and words != string
等等。。然而,这不起作用。你能帮我解决这个问题吗?也许我的代码总的来说是错的
这有点讨厌,但检查NUM可能会有用!:)
将问题归结为基本要素。 下面的问题应该是可行的,但是你的问题看起来很像家庭作业
结果:
如果你需要过滤数字
结果:
如果您需要拒绝函数中少于4个单词的数字/响应
相关问题 更多 >
编程相关推荐