如果对我的3条if语句的响应为no,如何打印消息?

2024-09-30 16:38:51 发布

您现在位置:Python中文网/ 问答频道 /正文

如果用户对以上三个问题都回答“否”,我想打印一条消息,说要接听电话。如何编写程序来知道所有3个答案都是“否”并打印消息

asleep = input("Are you sleep?")
    if asleep == "yes":
        print("You will not answer the phone.")
    if asleep == "no":
        print("You will answer the phone.")

momCall = input("Is it mom calling?")
    if momCall == "yes":
        print("You will answer the phone.")
    if momCall == "no":
        print("You will not answer the phone.")

morning = input("Is it morning?")
    if morning = "yes":
        print("You will not answer the phone.")
    if morning = "no":
        print("You will answer the phone")

Tags: thenoansweryouinputifisnot
2条回答

您的输入应该是比较运算符。我相信你把“是”和“否”混为一谈了。一个建议是仔细阅读如何使用if/elif/else语句

asleep = input("Are you sleep?")
if asleep == "yes":
    print("You will not answer the phone.")
elif asleep == "no":
    print("You will answer the phone.")
else:
    pass

momCall = input("Is it mom calling?")
if momCall == "yes":
    print("You will answer the phone.")
elif momCall == "no":
    print("You will not answer the phone.")
else:
    pass
morning = input("Is it morning?")
if morning == "yes":
    print("You will not answer the phone.")
elif morning == "no":
    print("You will answer the phone")
else:
    pass

if asleep == 'no' and momCall == 'no' and morning == 'no':
    print("Answer the phone")
else:
    pass

您可以将所有答案添加到列表中,并验证所有元素是否相等

print(all('no' == _ for _ in x))

# 'x' is the list with every answer
# '_' will be a temporary variable to get every element from 'x'
# 'no' == _ for _ in x will return true if all the elements in 'x' are equal to 'no'
# otherwise, you can change 'no' to 'yes' if you want
# then, you just need to do an IF/ELSE statement and return something if TRUE or FALSE

if all('no' == _ for _ in x):
    print('Answer the phone')

# You can create an empty list by using 'variable_name' = []
# and add value to the list by using 'variable_name'.append()
# Ex:
# abc = []
# 'abc' will be a new empty list
# abc.append('a')
# 'abc' now will have a new element, that will be 'a'

相关问题 更多 >