python虽然循环功能多个条件
我正在尝试创建一个函数,该函数接受两个单词并在满足条件时返回它们。单词 1 必须包含一定数量的字符,单词 2 必须以特定字母开头。我可以让它在一个条件下工作,但当我必须满足两个条件时我会感到困惑。这是我到目前为止所拥有的。下面的例子
Enter a 4 letter word: two
Enter a 4 letter word: wall
Enter a word starting with B: apple
Enter a word starting with B: boxes
['wall', 'boxes']
def twoWords(lenLet, strtLet):
input1 = str(input("Enter a word that is 4 letters long: "))
while len(input1) != 4:
input1 = str(input("Enter a word that is 4 letters long: "))
if len(input1) == 4:
break
input2 = str(input("Enter a word that begins with b: "))
while firstletter(input2) != 'b' or 'B':
input2 = str(input("Enter a word that begins with b: "))
if firstletter(input2) == 'b' or 'B':
break
return input1 and input2
print(twoWords()
I am trying to create a function that takes two words and returns them if conditions are met. Word 1 has to be a certain number of characters, word 2 has to begin with a certain letter. I can get it to work with one condition but am confused when I have to meet two. This is what I have so far. Example below
Enter a 4 letter word: two
Enter a 4 letter word: wall
Enter a word starting with B: apple
Enter a word starting with B: boxes
['wall', 'boxes']
def twoWords(lenLet, strtLet):
input1 = str(input("Enter a word that is 4 letters long: "))
while len(input1) != 4:
input1 = str(input("Enter a word that is 4 letters long: "))
if len(input1) == 4:
break
input2 = str(input("Enter a word that begins with b: "))
while firstletter(input2) != 'b' or 'B':
input2 = str(input("Enter a word that begins with b: "))
if firstletter(input2) == 'b' or 'B':
break
return input1 and input2
print(twoWords()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的方向是正确的,但您无法执行
firstletters(input) == 'b' 或 'B'
。你可以做的是转换为小写,然后使用input2[0].lower() != 'b'
检查 'b' 或者你可以使用input2[0] not in ('b', 'B')
检查两者。旁注:您不需要将
input()
的结果转换为str
,因为它已经是str
。我不确定你想要返回什么,但是input1 和 input2
没有多大意义。如果您想返回两个单词(在 元组 中),请使用return (输入1,输入2)
。如果您只是想说输入正确,您可以使用return True
。仅当两个单词都满足您的条件时才会到达 return 语句,否则您的程序将陷入无限循环。请注意,我已将您的
firstletters()
函数替换为适用于第一个字符的input[0]
函数。如果您想检查几个字符,也可以使用str.startswith()
。You were on the right track, but you can't do
firstletters(input) == 'b' or 'B'
. What you could to is transform to lower case and then check against 'b' usinginput2[0].lower() != 'b'
or you could useinput2[0] not in ('b', 'B')
to check for both.A side note: You don't need to cast the result of
input()
tostr
as it already is astr
. And I am not sure what you want to return butinput1 and input2
does not make much sense. If you want to return both words (in a tuple) usereturn (input1, input2)
. If you just want to say that the input was correct you could usereturn True
. The return statement will only be reached if both words meet your conditions otherwise your program will be in an infinite loop.Please note, that I have replaced your
firstletters()
function withinput[0]
which works for the first character. If you want to check for a couple of characters you could also usestr.startswith()
.