Python3:为什么我的 else 语句激活,而不管多个 if 语句是否为真


tweet = ''
tweet = input('Enter a tweet (160 characters or less):n')
if len(tweet) <= 160:
if 'LOL' in tweet:
print('Laugh out loud')
if 'BFN' in tweet:
print('Bye for now')
if 'BRB' in tweet:
print('Be right back')
if 'IRL' in tweet:
print('In real life')
if 'FTW' in tweet:
print('For the win')
if 'LMFAO' in tweet:
print('Laugh my F**king a** off')
if 'LMAO' in tweet:
print('Laugh my a** off')
else:
print('You didn't use any abbreviations')
elif len(tweet) == 0:
print('Wow, such empty')
elif len(tweet) > 160:
print('Your tweet is over 160 characters')

因此,例如,如果我运行此程序并输入: 哈哈

输出为: 大声笑 马上回来 您没有使用任何缩写

而我想要的该输入的输出是: 大声笑 马上回来

据我所知,缩进是正确的。此外,我们还没有讨论"break"或"枚举"函数,因此我们不需要使用这些函数来获得所需的结果。

谢谢你的帮助!!

这是在大家的帮助下正常工作的代码:

tweet = ''
tweet = input('Enter a tweet (160 characters or less):n')
tweet_abbrev = False
if len(tweet) <= 160:
if 'LOL' in tweet:
tweet_abbrev = True
print('Laugh out loud')
if 'BFN' in tweet:
tweet_abbrev = True
print('Bye for now')
if 'BRB' in tweet:
tweet_abbrev = True
print('Be right back')
if 'IRL' in tweet:
tweet_abbrev = True
print('In real life')
if 'FTW' in tweet:
tweet_abbrev = True
print('For the win')
if 'LMFAO' in tweet:
tweet_abbrev = True
print('Laugh my F**king a** off')
if 'LMAO' in tweet:
tweet_abbrev = True
print('Laugh my a** off')
if not tweet_abbrev:
print('You didn't use any abbreviations')
if len(tweet) == 0:
print('Wow, such empty')
elif len(tweet) > 160:
print('Your tweet is over 160 characters')

谢谢大家的快速回复!!

您可能想要的是以下内容:

tweet = ''
tweet = input('Enter a tweet (160 characters or less):n')
if len(tweet) <= 160:
abbr_found = False
for (abbr, feedback) in [
('LOL', 'Laugh out loud'),
('BFN', 'Bye for now'),
('BRB', 'Be right back'),
('IRL', 'In real life'),
('FTW', 'For the win'),
('LMFAO', 'Laugh my F**king a** off'),
('LMAO', 'Laugh my a** off'),
]:
if abbr in tweet:
print(feedback)
abbr_found = True
if not abbr_found:
print('You didn't use any abbreviations')
elif len(tweet) == 0:
print('Wow, such empty')
elif len(tweet) > 160:
print('Your tweet is over 160 characters')

对于您检查的每个缩写,您都可以获得"翻译"为人类语言的反馈。如果未使用缩写,您将收到指示这一点的反馈。

我在这里对你想要的逻辑做了一个假设,实现它的最简单方法是使用这个循环。否则,除了大量的if语句之外,您还必须在每个条件块中分配给addr_found

这是因为您的最后一个if语句会进行检查,如果不正确,则默认为"您没有使用任何缩写"。

您不希望所有语句都elif,因为这样它只会解析为第一次检查,这是真的。您要单独检查它们,如果为真,则打印相应的语句。

你只是错过了一件事:检查你的字符串是否没有使用任何缩写。试试这个。

tweet = ''
tweet = input('Enter a tweet (160 characters or less):n')
if len(tweet) <= 160:
if 'LOL' in tweet:
print('Laugh out loud')
if 'BFN' in tweet:
print('Bye for now')
if 'BRB' in tweet:
print('Be right back')
if 'IRL' in tweet:
print('In real life')
if 'FTW' in tweet:
print('For the win')
if 'LMFAO' in tweet:
print('Laugh my F**king a** off')
if 'LMAO' in tweet:
print('Laugh my a** off')
if {Insert check to see if there are any abbreviations}:
print('You didn't use any abbreviations')
elif len(tweet) == 0:
print('Wow, such empty')
elif len(tweet) > 160:
print('Your tweet is over 160 characters')

只需将{Insert check to see if there are any abbreviations}替换为有效的支票即可。要立即试用,您可以执行if ''' in tweet:

我会将这些if...if...if...else更改为if..elif...elif...else。可能存在一些逻辑错误。

否则只考虑最后一个 if 语句。如果您希望它考虑所有内容,请改用 elif。

编辑:在这种情况下,您的程序将在第一个 true 语句后结束。如果您想检查推文中的所有单词,请使用

words = tweet.split()

并为 for 循环中的每个单词执行代码(使用 elifs(。

跟踪满足了多少个if条件,最后,不要使用else,检查满足条件的数量是否大于 0。如果没有,则您没有使用任何缩写。

或者,您可以使用布尔值或其他东西,这取决于您。

编辑:未注释的代码用于布尔值,注释显示了如何使用计数器执行相同的操作。

x = input("enter numbers")
# number_of_numbers = 0
valid_number = False
if "1" in x:
valid_number = True
# number_of_numbers += 1
print("1")
if "2" in x:
valid_number = True
# number_of_numbers += 1
print("2")
...
# if not number_of_numbers:
if not valid_number:
print("You didn't use any numbers")

我知道它可能不是那么有效,但你可以初始化一个flag

tweet = ''
tweet = input('Enter a tweet (160 characters or less):n')
flag = 0
if len(tweet) <= 160:
if 'LOL' in tweet:
print('Laugh out loud')
flag = 1;
if 'BFN' in tweet:
print('Bye for now')
flag = 1;
if 'BRB' in tweet:
print('Be right back')
flag = 1;
if 'IRL' in tweet:
print('In real life')
flag = 1;
if 'FTW' in tweet:
print('For the win')
flag = 1;
if 'LMFAO' in tweet:
print('Laugh my F**king a** off')
flag = 1;
if 'LMAO' in tweet:
print('Laugh my a** off')
flag = 1;
elif flag==0:
print('You didn't use any abbreviations')
elif len(tweet) == 0:
print('Wow, such empty')
elif len(tweet) > 160:
print('Your tweet is over 160 characters')

最新更新