Python While循环直到0

时间:2020-06-09 18:22:05

标签: python loops while-loop

到目前为止,我没有这段代码,但是它要求我使用while循环重复询问用户一个分母,只要分母为0。编辑:我相信我缺少的代码介于两者之间分母=,如果分子= int(输入...

nginx-ingress-controller

2 个答案:

答案 0 :(得分:2)

使用while循环:并读取break instruction

numerator = int(input("Enter a numerator: "))
while True:
   denominator = int(input("Enter denominator: "))
   if denominator != 0:
       break

if numerator / denominator * denominator == numerator:
    print "Divides evenly!"
else:
    print "Doesn't divide evenly."

答案 1 :(得分:2)

使用tryexcept试用此代码以捕获除以0的错误

代码:

while True:
    numerator = int(input("Enter a numerator: "))

    denominator = int(input("Enter denominator: "))

    try:
        if numerator / denominator * denominator == numerator:
            print "Divides evenly!"
        else:
            print "Doesn't divide evenly."
    except:
        print "Sorry demoninator cannot be zero"

但这在python 2.x中工作吗?我不确定。试试看