首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我有这个while循环: while x == False or y != 10:,但当Y变为10时,它会继续运行循环

我有这个while循环: while x == False or y != 10:,但当Y变为10时,它会继续运行循环
EN

Stack Overflow用户
提问于 2019-11-29 03:41:26
回答 1查看 48关注 0票数 1

我有这段代码,但当Y变为10时,它会一直运行循环,为什么?我是python的初学者,所以请不要认为我很愚蠢或者别的什么:)

代码语言:javascript
复制
def destiny_calculator():
    mass = str(input("enter the mass of your material (it could be in gram, milligram, kilogram or ton "))
    if mass.isdigit() == False:
        try:
            float(mass)
        except Exception:
            x = False
            y = 0
            while x == False or y != 10:
                print("Please enter JUST a number")
                mass = str(input("enter the mass of your material (it could be in gram, milligram, kilogram or ton - "))
                y = y + 1
                print (y)
            if y == int(10):
                print("Too many errors")
                exit()
            else:
                pass
        else:
            pass
EN

回答 1

Stack Overflow用户

发布于 2020-07-08 07:02:04

由于您使用的是or,因此代码只检查其中一个条件是否满足。因此,只要x == False为真,代码就会运行。如果您希望代码在y == 10时结束,那么应该改用and操作符。这只会在两个条件都为真的情况下运行代码,并且只要其中一个条件为假(例如y == 10),代码就不会运行。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59095335

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档