【问题标题】:How to i make this while loop " not equal to" [duplicate]我如何使这个while循环“不等于” [重复]
【发布时间】:2020-03-07 23:19:12
【问题描述】:

所以我正在为一个学校项目编写一小段代码,但我不确定这个 while 命令是否可行:

array = []


list_amount = int(input("Enter how many numbers will be in the list"))

while len(array) == list_amount:
    array.append(int(input("Enter a number")))

在线,while len(array) == list_amount: 我希望它不等于

这样它就会一直让你添加数字,直到数组的长度和你输入的数量相同,然后循环就会中断。

【问题讨论】:

  • 您熟悉!=(“不等于”运算符)吗?
  • 你试过谷歌搜索Not equal in python吗?

标签: python while-loop


【解决方案1】:

== 替换为!=!= 是“不等于”的python 语法。

【讨论】:

    【解决方案2】:

    使用!= 运算符:

    array = []
    
    list_amount = int(input("Enter how many numbers will be in the list"))
    
    while len(array) != list_amount:
        array.append(int(input("Enter a number")))
    

    【讨论】:

      【解决方案3】:

      只需使用!= 而不是==。它的意思是not equal to,基本上与==相反。

      【讨论】:

        【解决方案4】:
        array = []
        
        list_amount = int(input("Enter how many numbers will be in the list"))
        
        for i in range(list_amount):
            array.append(int(input("Enter a number")))
        

        【讨论】:

        • 当然就是这么简单!非常感谢
        猜你喜欢
        • 2014-03-28
        • 2020-10-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多