我正在学习python。我写这段代码。我尝试使用main来转换函数中的这个python代码。我知道该怎么做。有人可以帮助我。我尝试了3个多小时。现在我的思绪停止了。我真的很感激你的帮助。
isExit = 0
count = 0
total_int = 0
而True:
total_int = 0
count = 0
print ("\n")
print ("1) Credit Card Payoff Summary")
print ("2) Credit Card Payoff Details")
print ("3) Exit ")
choice = (int)(input("Enter your choice - "))
if(choice == 1):
balance = (float)(input("Enter your credit card balance - "))
apr = (float)(input("Enter Annual Percentage Rate - "))
print ("1) Interest on current balance + 1% of current balance.")
print ("2) 2% of current balance.")
select = (int)(input("Select your choice - "))
if(select == 1):
if(balance <= 0 or apr <= 0) : #checking if balance or APR is 0 or not
print ("Interest cannot be calculated as balance or APR is 0.")
else :
while (balance > 0) :
m_interest = float(balance) * (apr/ 1200)
min_month_pay = m_interest + (0.01 * balance)
if (min_month_pay < 15):
min_month_pay = 15
principal_paid = min_month_pay - m_interest
balance = balance - principal_paid
total_int += m_interest
count += 1
years = (float)(count / 12)
print ("Years required to payoff card : ", round(years, 2)) #round() is used to format the float number upto 2 decimal place.
print ("Total interest paid : ", round(total_int,2))
elif(select == 2):
if(balance <= 0 or apr <= 0) :
print ("Interest cannot be calculated as balance or APR is 0.")
else :
while (balance > 0) :
m_interest = float(balance) * (apr/ 1200)
min_month_pay = 0.02 * balance
if (min_month_pay < 15):
min_month_pay = 15
principal_paid = min_month_pay - m_interest
balance = balance - principal_paid
total_int += m_interest
count += 1
years = (float)(count / 12)
print ("Years required to payoff card : ", round(years, 2))
print ("Total interest paid : ", round(total_int, 2))
else :
print ("Invalid selection.")
if(choice == 2):
balance = (float)(input("Enter your credit card balance - "))
apr = (float)(input("Enter Annual Percentage Rate - "))
print ("1) Interest on current balance + 1% of current balance.")
print ("2) 2% of current balance.")
select = (int)(input("Select your choice - "))
if(select == 1):
if(balance <= 0 or apr <= 0) :
print ("Interest cannot be calculated as balance or APR is 0.")
else :
print ("Month Min. Int. Princ. Rem. Pay. Paid Paid Bal.") #headers of the detail
while (balance > 0) :
m_interest = float(balance) * (apr/ 1200)
min_month_pay = m_interest + (0.01 * balance)
if (min_month_pay < 15):
min_month_pay = 15
principal_paid = min_month_pay - m_interest
balance = balance - principal_paid
total_int += m_interest
count += 1
print (count, " ", round(min_month_pay, 2), " ", round(m_interest, 2), " ", round(principal_paid, 2), " ", round(balance, 2)) #This will print the detail line by line
years = (float)(count / 12)
print ("Years required to payoff card : ", round(years, 2))
print ("Total interest paid : ", round(total_int, 2))
if(select == 2):
if(balance <= 0 or apr <= 0) :
print ("Interest cannot be calculated as balance or APR is 0.")
else :
print ("Month Min. Int. Princ. Rem. Pay. Paid Paid Bal.")
while (balance > 0) :
m_interest = float(balance) * (apr/ 1200)
min_month_pay = 0.02 * balance
if (min_month_pay < 15):
min_month_pay = 15
principal_paid = min_month_pay - m_interest
balance = balance - principal_paid
total_int += m_interest
count += 1
print (count, " ", round(min_month_pay, 2), " ", round(m_interest, 2), " ", round(principal_paid, 2), " ", round(balance, 2))
years = (float)(count / 12)
print ("Years required to payoff card : ", round(years, 2))
print ("Total interest paid : ", round(total_int, 2))
else :
print ("Invalid selection.")
if (choice == 3):
print ("Application is terminating now.")
isExit = 1
else:
print ("Invalid Choice.")
if(isExit):
break
答案 0 :(得分:0)
您正在寻找此声明
if __name__ == '__main__':
选择所有代码ctrl+a
,按tab
缩进,然后将上述语句放在代码的开头
<强>解释强>
当Python解释器读取python文件时,该文件是2个中的一个,它是带有main方法的程序,或者是一个其他主文件导入的模块。此属性存储在名为__name__
的变量中。如果__name__
的值等于'__main__'
,则它向解释器指示该文件旨在作为程序(main)运行。否则,该文件很可能是从其他程序导入的。
虽然据我所知,你实际上并不需要这个陈述。如果你只是按顺序运行一堆代码而不使用任何主声明,那么这也很好。大多数人把它放在文件的底部,然后调用上面声明的函数,如下所示:
def func1():
print("function 1")
some_string = "hello"
return some_string
def func2():
print("function 2")
if __name__ == '__main__':
print("here is the main method where you can call functions above")
results = func1()
func2()