简体   繁体   中英

Python While loop Until 0

I have this code down so far but it is asking me to use a while loop to repeatedly ask the user for a denominator for as long as the denominator is 0. Edit: I believe the code I'm missing goes in between the denominator= and if numerator = int(input...

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


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

use a while loop: and read 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."

Try this code out using try and except to catch the error of division by 0

CODE:

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"

But does this work in python 2.x? im not sure. Give it a try

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM