python名称错误:名称';还清债务';没有定义
当我以这种方式尝试代码时,效果很好:python名称错误:名称';还清债务';没有定义,python,undefined,nameerror,Python,Undefined,Nameerror,当我以这种方式尝试代码时,效果很好: #this block of code will get the necessary inputs from the user or_balance=balance=float(input('Enter the outstanding balance on your credit card: ')) int_rate=float(input('Enter the annual credit card interest rate as a decimal:
#this block of code will get the necessary inputs from the user
or_balance=balance=float(input('Enter the outstanding balance on your credit card: '))
int_rate=float(input('Enter the annual credit card interest rate as a decimal: '))
mon_int_rate=int_rate/12
#this block of code will deal with the inputs to get the outputs
mon_pay_low_bound=balance/12
mon_pay_up_bound=(balance*(1+mon_int_rate)*12)/12
def total_balance(mon_pay):
balance=or_balance
global num
num=0
while num<12:
num+=1
balance=balance*(1+mon_int_rate)-mon_pay
return balance
#this block of code will get the result desired by using the bisection method
a=mon_pay_low_bound
b=mon_pay_up_bound
tol=.0000000000005
n=1
while n<999:
c=(a+b)/2
if total_balance(c)==0 or (b-a)/2<tol:
break
else:
n+=1
if total_balance(c)<0:
b=c
else:
a=c
#this block of code will give the final results
print('RESULT')
print('Monthly payment to pay off debt in 1 year:',round(c,2))
print('Number of months needed:',num)
print('Balance',round(total_balance(c),2))
#此代码块将从用户处获得必要的输入
或者_balance=balance=float(输入('在您的信用卡上输入未付余额:'))
int_rate=float(输入('以十进制形式输入信用卡年利率:'))
mon_int_rate=int_rate/12
#这段代码将处理输入以获得输出
mon\u pay\u low\u bound=余额/12
周一支付上限=(余额*(1+周一国际汇率)*12)/12
def总余额(周一支付):
平衡
全局数
num=0
而num如果这个总平衡(c)==0或(b-a)/2如其他人所说,这是因为总平衡(c)==0或(b-a)/2定义了while之外的变量loop@matcheek问题不在于while
循环,而是他在if/else
中设置了变量。如果If
的计算结果从未为true,则从未设置变量,这可能是名称错误的原因
while n<999:
c=(a+b)/2
if total_balance(c)==0 or (b-a)/2<tol:
pay_off=c
break
else:
n+=1
if total_balance(c)<0:
b=c
else:
a=c
#this block of code will give the final results
print('RESULT')
print('Monthly payment to pay off debt in 1 year:',round(pay_off,2))
print('Number of months needed:',num)
print('Balance',round(total_balance(pay_off),2))
pay_off=0
while n<999:
c=(a+b)/2
if total_balance(c)==0 or (b-a)/2<tol:
pay_off=c
break
else:
n+=1
if total_balance(c)<0:
b=c
else:
a=c
if pay_off == 0:
print "I couldn't determine the payoff amount in %d iterations" % n