了解while循环中的变量和函数

问题描述 投票:0回答:1

我有一个函数,它使用了一个while循环,它是这样的

def fun_rightamount(monthly_salary,down_payment,semi_annual_rise,r,errore=False):
    epsilon = 100
    num_guesses = 0 
    low = 0 
    high = 100
    portion_saved = (high + low)/2.0    
    while abs(fun_thirtysix(portion_saved,monthly_salary,down_payment,semi_annual_rise,r) - down_payment ) >= epsilon : 
        if fun_thirtysix(portion_saved,monthly_salary,down_payment,semi_annual_rise,r)  < down_payment  :             
            low = portion_saved        
        else: 
            high = portion_saved         
        portion_saved = (high + low)/2.0                
        num_guesses += 1 
        if portion_saved > 99.9999:            
            print('attenzione ERRORE !!!!!!!!!!!!!!!!!!!!!!!!!!')
            errore=True
            break    
    return portion_saved,num_guesses,errore

我可以用另一个函数'pippo'来改变while状态。

def pippo(portion_saved,monthly_salary,down_payment,semi_annual_rise,r):
    yy=(fun_thirtysix(portion_saved,monthly_salary,down_payment,semi_annual_rise,r) - down_payment) 
    return yy

def fun_rightamount(monthly_salary,down_payment,semi_annual_rise,r,errore=False):
    epsilon = 100
    num_guesses = 0 
    low = 0 
    high = 100
    portion_saved = (high + low)/2.0    
    while abs(pippo(portion_saved,monthly_salary,down_payment,semi_annual_rise,r)) >= epsilon :  #
        if pippo(portion_saved,monthly_salary,down_payment,semi_annual_rise,r)  < 0  :           #  
            low = portion_saved                                                                  #
        else: 
            high = portion_saved         
        portion_saved = (high + low)/2.0                
        num_guesses += 1 
        if portion_saved > 99.9999:            
            print('attenzione ERRORE !!!!!!!!!!!!!!!!!!!!!!!!!!')
            errore=True
            break    
    return portion_saved,num_guesses,errore

一切都和预期的一样,两个结构的工作原理完全一样,但是如果我尝试在我的while语句中使用一个变量('yy')而不是一个函数,我的程序就会进入一个无限循环。

def fun_rightamount(monthly_salary,down_payment,semi_annual_rise,r,errore=False):
    epsilon = 100
    num_guesses = 0 
    low = 0 
    high = 100
    portion_saved = (high + low)/2.0
    yy=(fun_thirtysix(portion_saved,monthly_salary,down_payment,semi_annual_rise,r) - down_payment) # 
    while abs(yy) >= epsilon :                                                                      #
        if yy  < 0  :                                                                               #     
            low = portion_saved        
        else: 
            high = portion_saved         
        portion_saved = (high + low)/2.0                
        num_guesses += 1 
        if portion_saved > 99.9999:            
            print('attenzione ERRORE !!!!!!!!!!!!!!!!!!!!!!!!!!')
            errore=True
            break    
    return portion_saved,num_guesses,errore

while循环,循环,但num_guesses incraes,abs(yy)值没有改变,section_saved保持'0',而至少在第一个循环中应该是'50'。有什么办法吗?

python-3.x function variables while-loop
1个回答
1
投票

绿色斗篷男 回答了问题

yy会被设置一次,当你分配它时,然后永远不会改变。即使partic_saved会改变,你也需要再次运行函数,并再次将其赋值给yy。在你之前的例子中,你在每次迭代时都调用了这个函数,所以它的终止是正确的--绿色斗篷男 4月29日 19:07分

© www.soinside.com 2019 - 2024. All rights reserved.