如何在 python 中换行?
How to do a line break in python?
这是我关于 Whosebug 的第一个问题。
我正在尝试在 Python 中在线 OCW 类 上做习题集,但我不知道如何调试我的代码。我使用 IDLE,并且 Python 2.7.
这是我的代码:
balance = float(raw_input("Enter the outstanding balance on your credit card: "))
annual_itst = float(raw_input("Enter the annual credit card interest rate as a decimal: "))
min_paymt_rate = float(raw_input("Enter the minimum monthly payment rate as a decimal: "))
remaining_balance = balance
for i in range(1,13):
min_monthly_paymt=min_paymt_rate*remaining_balance
inst_paid = (annual_itst / 12.0 )*remaining_balance
pcpl_paid = min_monthly_pamt - inst_paid
remaining_balance -= pcpl_paid
print("Month: " & i, \n "Minimum monthly payment: " & "$" & round(min_monthly_paymt, 2), \n "Principle paid: " & "$" & round(pcpl_paid, 2),
\n "Remaining balance: " & "$" & round(remaining_balance, 2))
当我执行它时,我在 print
函数的第一行之后收到一条消息错误 "unexpected character after line continuation character",红色突出显示。
感谢您的帮助!
要打印换行符,请使用包含换行符的字符串:"\n"
。
要打印出您想要的复杂字符串,您可以使用多个 print
语句(通常会添加它们自己的换行符),或者 assemble 您的字符串然后打印它。
print("Month:", i)
print("Minimum monthly payment: $" + round(min_monthly_paymt, 2))
(etc.)
您可以将字符串加在一起形成更长的字符串,但不能将字符串和整数加在一起。为此,您需要将数字转换为字符串或了解格式化语句。
数字到字符串:
print "Month: "+str(i)
字符串格式:
report = """Month: %d
Minimum monthly payment: $%.2f
Principal paid: $%.2f
Remaining balance: %.2f"""
print report % (i, min_monthly_paymt, pcpl_paid, remaining_balance)
阅读所有这些方法;它们在不同情况下很有用。
在这里,
了解运算符和格式以打印输出
balance = float(raw_input("Enter the outstanding balance on your credit card: "))
annual_itst = float(raw_input("Enter the annual credit card interest rate as a decimal: "))
min_paymt_rate = float(raw_input("Enter the minimum monthly payment rate as a decimal: "))
remaining_balance = balance
for i in range(1,13):
min_monthly_paymt=min_paymt_rate*remaining_balance
inst_paid = (annual_itst / 12.0 )*remaining_balance
pcpl_paid = min_monthly_paymt - inst_paid
remaining_balance -= pcpl_paid
print "\n\n\n"
print "Month: {}".format(i), '\n',"Minimum monthly payment: $ {}".format(round(min_monthly_paymt, 2)), '\n',"Principle paid:$ ".format(round(pcpl_paid, 2)),'\n',"Remaining balance: $".format(round(remaining_balance, 2))
您必须将 int 转换为 string 才能连接,并且您可以分别打印每一行以在不同的行中打印。
balance = float(raw_input("Enter the outstanding balance on your credit card: "))
annual_itst = float(raw_input("Enter the annual credit card interest rate as a decimal: "))
min_paymt_rate = float(raw_input("Enter the minimum monthly payment rate as a decimal: "))
remaining_balance = balance
for i in range(1,13):
min_monthly_paymt=min_paymt_rate*remaining_balance
inst_paid = (annual_itst / 12.0 )*remaining_balance
pcpl_paid = min_monthly_paymt - inst_paid
remaining_balance -= pcpl_paid
print("Month: " + str(i))
print("Minimum monthly payment: $" +str(round(min_monthly_paymt, 2)))
print("Principle paid: $" + str(round(pcpl_paid, 2)))
print("Remaining balance: $" + str(round(remaining_balance, 2)))
这是我关于 Whosebug 的第一个问题。 我正在尝试在 Python 中在线 OCW 类 上做习题集,但我不知道如何调试我的代码。我使用 IDLE,并且 Python 2.7.
这是我的代码:
balance = float(raw_input("Enter the outstanding balance on your credit card: "))
annual_itst = float(raw_input("Enter the annual credit card interest rate as a decimal: "))
min_paymt_rate = float(raw_input("Enter the minimum monthly payment rate as a decimal: "))
remaining_balance = balance
for i in range(1,13):
min_monthly_paymt=min_paymt_rate*remaining_balance
inst_paid = (annual_itst / 12.0 )*remaining_balance
pcpl_paid = min_monthly_pamt - inst_paid
remaining_balance -= pcpl_paid
print("Month: " & i, \n "Minimum monthly payment: " & "$" & round(min_monthly_paymt, 2), \n "Principle paid: " & "$" & round(pcpl_paid, 2),
\n "Remaining balance: " & "$" & round(remaining_balance, 2))
当我执行它时,我在 print
函数的第一行之后收到一条消息错误 "unexpected character after line continuation character",红色突出显示。
感谢您的帮助!
要打印换行符,请使用包含换行符的字符串:"\n"
。
要打印出您想要的复杂字符串,您可以使用多个 print
语句(通常会添加它们自己的换行符),或者 assemble 您的字符串然后打印它。
print("Month:", i)
print("Minimum monthly payment: $" + round(min_monthly_paymt, 2))
(etc.)
您可以将字符串加在一起形成更长的字符串,但不能将字符串和整数加在一起。为此,您需要将数字转换为字符串或了解格式化语句。
数字到字符串:
print "Month: "+str(i)
字符串格式:
report = """Month: %d
Minimum monthly payment: $%.2f
Principal paid: $%.2f
Remaining balance: %.2f"""
print report % (i, min_monthly_paymt, pcpl_paid, remaining_balance)
阅读所有这些方法;它们在不同情况下很有用。
在这里, 了解运算符和格式以打印输出
balance = float(raw_input("Enter the outstanding balance on your credit card: "))
annual_itst = float(raw_input("Enter the annual credit card interest rate as a decimal: "))
min_paymt_rate = float(raw_input("Enter the minimum monthly payment rate as a decimal: "))
remaining_balance = balance
for i in range(1,13):
min_monthly_paymt=min_paymt_rate*remaining_balance
inst_paid = (annual_itst / 12.0 )*remaining_balance
pcpl_paid = min_monthly_paymt - inst_paid
remaining_balance -= pcpl_paid
print "\n\n\n"
print "Month: {}".format(i), '\n',"Minimum monthly payment: $ {}".format(round(min_monthly_paymt, 2)), '\n',"Principle paid:$ ".format(round(pcpl_paid, 2)),'\n',"Remaining balance: $".format(round(remaining_balance, 2))
您必须将 int 转换为 string 才能连接,并且您可以分别打印每一行以在不同的行中打印。
balance = float(raw_input("Enter the outstanding balance on your credit card: "))
annual_itst = float(raw_input("Enter the annual credit card interest rate as a decimal: "))
min_paymt_rate = float(raw_input("Enter the minimum monthly payment rate as a decimal: "))
remaining_balance = balance
for i in range(1,13):
min_monthly_paymt=min_paymt_rate*remaining_balance
inst_paid = (annual_itst / 12.0 )*remaining_balance
pcpl_paid = min_monthly_paymt - inst_paid
remaining_balance -= pcpl_paid
print("Month: " + str(i))
print("Minimum monthly payment: $" +str(round(min_monthly_paymt, 2)))
print("Principle paid: $" + str(round(pcpl_paid, 2)))
print("Remaining balance: $" + str(round(remaining_balance, 2)))