python虽然循环功能多个条件

发布于 2025-01-17 18:19:10 字数 845 浏览 0 评论 0原文

我正在尝试创建一个函数,该函数接受两个单词并在满足条件时返回它们。单词 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悲喜皆因你 2025-01-24 18:19:10

您的方向是正确的,但您无法执行 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()

def twoWords(lenLet, strtLet):
    # no need to cast to str, input always returns a str
    input1 = input(f"Enter a word that is {lenLet} letters long: ")
    while len(input1) != 4:
        input1 = input(f"Try again. Enter a word that is {lenLet} letters long: ")
        if len(input1) == 4:
            break
    input2 = input(f"Enter a word that begins with {strtLet}: ")
    while input2[0].lower() != 'b':
        input2 = input(f"Try again. Enter a word that begins with {strtLet}: ")
        if input2[0].lower() == 'b':
            break
    # you will be stuck in an infinite loop as long as the conditions are not met
    # only when the conditions are met you will arrive here so you can just return true
    return True

print(twoWords(4, "b"))

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' using input2[0].lower() != 'b' or you could use input2[0] not in ('b', 'B') to check for both.

A side note: You don't need to cast the result of input() to str as it already is a str. And I am not sure what you want to return but input1 and input2 does not make much sense. If you want to return both words (in a tuple) use return (input1, input2). If you just want to say that the input was correct you could use return 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 with input[0] which works for the first character. If you want to check for a couple of characters you could also use str.startswith().

def twoWords(lenLet, strtLet):
    # no need to cast to str, input always returns a str
    input1 = input(f"Enter a word that is {lenLet} letters long: ")
    while len(input1) != 4:
        input1 = input(f"Try again. Enter a word that is {lenLet} letters long: ")
        if len(input1) == 4:
            break
    input2 = input(f"Enter a word that begins with {strtLet}: ")
    while input2[0].lower() != 'b':
        input2 = input(f"Try again. Enter a word that begins with {strtLet}: ")
        if input2[0].lower() == 'b':
            break
    # you will be stuck in an infinite loop as long as the conditions are not met
    # only when the conditions are met you will arrive here so you can just return true
    return True

print(twoWords(4, "b"))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文