Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python程序在某些数字上挂起_Python - Fatal编程技术网

Python程序在某些数字上挂起

Python程序在某些数字上挂起,python,Python,我制作了一个程序,将两个数字相除,然后以十进制形式返回答案。它适用于我测试过的所有数字,除了8和9以及两者的任意组合。任何关于为什么会发生这种情况的解释都是受欢迎的 cont = True while(cont): initChoice = 0 print " " print "What would you like to do?" print "1) Divide two numbers" print "2) Find the GCD of two nu

我制作了一个程序,将两个数字相除,然后以十进制形式返回答案。它适用于我测试过的所有数字,除了8和9以及两者的任意组合。任何关于为什么会发生这种情况的解释都是受欢迎的

cont = True
while(cont):
    initChoice = 0
    print " "
    print "What would you like to do?"
    print "1) Divide two numbers"
    print "2) Find the GCD of two numbers"
    print "3) Find the LCM of two numbers"
    print "0) Exit"
    initChoice = input("")
    if(initChoice == 1):
        #This hangs up when certain numbers are entered
        #It only seems to hang up when only 8 or 9 are involved
        #For example (8,9) (9,8) (88,89) (89,88) (98,99) (99,98)
        #It gets hung up on num1 = num1-num2
        num1 = raw_input("Please enter the first number: ")
        num2 = raw_input("Please enter the second number: ")
        num1 = float(num1)
        num2 = float(num2)
        numer = num1
        denom = num2
        ans = 0.0
        iter1 = 0.0
        iter2 = 0.0
        while(num1 > 0):
            if(num1 >= num2):
                num1 = num1-num2
                iter1 += 1
            else:
                num2 = num2*0.1
                ans += iter1*(10**(-iter2))
                iter1 = 0
                iter2 += 1      
        ans += iter1*(10**(-iter2))
        print numer,"divided by",denom,"is",and

是的,我知道它将无限期地运行,我有更多的代码可以结束循环。

更改while循环条件,检查num2是否为零,就像“while(num1>0和num2>0):”。这应该行得通

而“while(num1>0)”无疑是个问题。它必须具有计算机上浮点变量的精度。从python中,您可以发现它正在导入sys,如下所示:

sys.float_info.min
因此,为了避免无限循环,您的代码可能与下面的代码类似

from sys import float_info

cont = True
while(cont):
    initChoice = 0
    print " "
    print "What would you like to do?"
    print "1) Divide two numbers"
    print "2) Find the GCD of two numbers"
    print "3) Find the LCM of two numbers"
    print "0) Exit"
    initChoice = input("")
    if(initChoice == 1):
        #This hangs up when certain numbers are entered
        #It only seems to hang up when only 8 or 9 are involved
        #For example (8,9) (9,8) (88,89) (89,88) (98,99) (99,98)
        #It gets hung up on num1 = num1-num2
        num1 = raw_input("Please enter the first number: ")
        num2 = raw_input("Please enter the second number: ")
        num1 = float(num1)
        num2 = float(num2)
        numer = num1
        denom = num2
        ans = 0.0
        iter1 = 0.0
        iter2 = 0.0

        precision=float_info.min

        while(num1 > 0+precision):
            if(num1 >= num2):
                num1 = num1-num2
                iter1 += 1
            else:
                num2 = num2*(0.1)
                ans += iter1*(10**(-iter2))
                iter1 = 0
                iter2 += 1
        ans += iter1*(10**(-iter2))
        print numer,"divided by",denom,"is",ans

    elif(initChoice == 0):
        break

它将无限期地运行,并被挂起;你是否在所有情况下都结束了循环<代码>在循环中打印“something”并查看它是否停止。我已经说过我知道这个部分会无限期运行,我没有发布整个代码,只是发布了导致问题的部分。我让它一直运行,直到用户选择零。这很有效,谢谢。我甚至没有想过考虑这个选择。再次感谢。num2在技术上永远不会达到零,因为每次num2>num1时,它只会被修改*0.1。