Python 如果任何问题的答案是否定的,那么如何打印特定文本

Python 如果任何问题的答案是否定的,那么如何打印特定文本,python,Python,我遇到了一个问题,问了5个问题。如果其中任何一个答案是否定的,它应该打印外星人!或者很酷。 到目前为止,我得到的是: human = input("Are you human? ") human = input("Are you living on planet Earth? ") human = input("Do you live on land? ") human = input("Have you eaten in the last year? ") human = input("Is

我遇到了一个问题,问了5个问题。如果其中任何一个答案是否定的,它应该打印外星人!或者很酷。 到目前为止,我得到的是:

human = input("Are you human? ")
human = input("Are you living on planet Earth? ")
human = input("Do you live on land? ")
human = input("Have you eaten in the last year? ")
human = input("Is 2 + 2 = 4? ")
if human == "yes":
    print("Cool")
elif human == "no":
    print("ALIEN!")`

您有一个变量human,它每次都会更改,并使用input Try和make获得一个新值,而不仅仅是使用human

human = input("Are you human? ")
var1 = input("Are you living on planet Earth? ")
var2 = input("Do you live on land? ")
var3 = input("Have you eaten in the last year? ")
var4 = input("Is 2 + 2 = 4? ")
if(human == "yes"):
    print("Cool")
elif(human == "no"):
    print("ALIEN!")
您可以使用any检查问题的任何答案是否为“否”,并相应地打印消息:

human = [input("Are you human? "), input("Are you living on planet Earth? "), 
         input("Do you live on land? "), input("Have you eaten in the last year? "), input("Is 2 + 2 = 4? ")]

if any(x.lower() == 'no' for x in human):
    print('ALIEN!')
else:
    print('Cool')

如果您不担心存储变量,只关心其中一个问题是否或n:

x=["human?","on earth?", "on land?","someone who eats food?","sure that 2+2=4?"]

for i in x:
    if input("Are you {}".format(i)).lower() in ['no','n']:
        print("Alien!")
        break
    else:
        print("Cool")

只是一个旁注:在这里,您可以看到使用for循环的一个很好的例子,因为有些代码重复了很多次。就我个人而言,我会采取以下措施来解决这个问题

创建问题列表 反复浏览问题列表 如果答案是否定的,请打断并打印Alien。 现在来看看代码:

# Initialize our list
lst = ["Are you human? ", "Are you living on planet Earth? ","Do you live on 
land? ","Have you eaten in the last year? ","Is 2 + 2 = 4? "]

#Create a flag for Alien
flag = False

#Iterate through the list
for question in lst: 
  answer = input(question)
  if answer == 'no':

    #Print alien
    print('Alien')

    #Change Flag
    flag = True

    #Break out of the loop
    break
#Check to see if our flag was thrown during the loop
if flag == False:
  print('cool')

如果您想在解决像这样的编码难题方面获得更多帮助,请查看本python简介课程:

您想打印Cool/ALIEN吗!在问完这五个问题后,还是在得到否定的答案后立即停止提问?如果你的回答既不是肯定的,也不是否定的,那会发生什么呢?你的病情将根据最后的回答进行评估。i、 e.输入2+2=4?jdehesa在所有5个问题都被问到之后,即使1个问题的答案是否定的,它也应该打印ALIEN!否则,如果我为第一个问题输入yes,为RestThank输入no,该怎么办?谢谢,但它甚至不起作用。上面的一个可以。你使用的是什么版本的python?@Chirag它不会改变,因为它要求的是人的价值,而不是其他变量。你不需要Python3.7或任何版本的Parantosis。另外,请再读一遍问题。