【问题标题】:How to make it so if any variable in a list is greater than an integer, it will print out something, else it will print out something else?如何做到这一点,如果列表中的任何变量大于整数,它将打印出一些东西,否则它会打印出其他东西?
【发布时间】:2022-01-21 04:25:49
【问题描述】:
one = int(input("Type a number"))
two = int(input("Type a second number"))
three = int(input("Type a third number"))
four = int(input("Type fourth number"))
num = [one,two,three,four]

如果num中的任何变量大于或等于7,它会打印(“yes”),否则,它会打印(“no”)?另外我不确定我是否正确列出了列表。

【问题讨论】:

    标签: python python-3.x list


    【解决方案1】:

    如果使用 max() 函数,则不需要使用任何 for 循环。

    num = [1, 9, 2, 6, 10]
    
    if max(num) >= 7:
        print("Yes")
    else:
        print("No")
    

    【讨论】:

      【解决方案2】:

      好消息 - 该列表看起来正确!

      您正在寻找的是两件事 - for 循环和条件。

      for 循环会查看列表中的每一项,条件语句会检查当前数字是否大于或等于 7。如果是,它可以更新一些值(这里是一个布尔值,用于评估是否一个大于 7 的数字)稍后再次检查。这是我对您问题的解决方案!

      # Assume all of the stuff from your question goes here.
      isGreaterThan = False
      for number in num:
          if number >= 7:
              isGreaterThan = True
              break
      if isGreaterThan:
          print("Yes!")
      else:
          print("No.")
      

      如果您不明白其中的任何部分的作用,请询问!

      【讨论】:

      • 我喜欢这个想法,但实现全错了。例如,True 而不是 true。另一方面,当然,您的意思是在循环中使用number
      • 感谢您指出这一点!我已根据需要进行了编辑。再次,谢谢。我不想提供不好/无效的答案,所以我欢迎编辑。
      【解决方案3】:

      您可以使用 for 循环遍历列表中的每个项目,以检查项目是否大于或等于 7。然后使用布尔值打印出正确的输出

      isGreaterThan = False
      for i in num:
           if i >= 7:
              isGreaterThan = True
              break
          
      if isGreaterThan:
          print("Yes")
      else:
          print("No")
      

      【讨论】:

        【解决方案4】:
        for n in num: # loop through all the elements in the list
        if(n > 7): # each element is in the "n" variable - cehck if n>7
            print("yes") # if yes, then pront it
            break # break the loop and stop looking
        

        【讨论】:

          【解决方案5】:

          以下是速记方式:

          num = [1, 9, 2]
          
          print("Yes" if any(n >= 7 for n in num) else "No")
          

          或者,按照另一个答案的建议,使用内置 max

          print("Yes" if max(num) >= 7 else "No")
          

          为了快速测试,我建议您降低列表中的第二个元素,并检查输出的变化情况。

          【讨论】:

            【解决方案6】:
                one = int(input("Type a number"))
            two = int(input("Type a second number"))
            three = int(input("Type a third number"))
            four = int(input("Type fourth number"))
            num = [one,two,three,four]
            x= 7
            for i in num:
                if i>x:
                    print(f"your  {i}th number is grater than {i} ")
                else:
                    pass
                x = x +1
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2015-10-14
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2021-07-06
              相关资源
              最近更新 更多